What does FragmentManager and FragmentTransaction exactly do?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 05:10:19

问题


I have simple code below

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

What do these lines of code do?


回答1:


getFragmentManager()

Return the FragmentManager for interacting with fragments associated with this activity.

FragmentManager which is used to create transactions for adding, removing or replacing fragments.

fragmentManager.beginTransaction();

Start a series of edit operations on the Fragments associated with this FragmentManager.

The FragmentTransaction object which will be used.

fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);

Replaces the current fragment with the mFeedFragment on the layout with the id: R.id.fragment_container

fragmentTransaction.addToBackStack(null);

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Useful for the return button usage so the transaction can be rolled back. The parameter name:

Is an optional name for this back stack state, or null.

See for information the other question What is the meaning of addToBackStack with null parameter?

The Last statement commits the transaction and executes all commands.

See the google documentation for more help:

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/FragmentTransaction.html




回答2:


Android FragmentManager

A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.

Android FragmentTransaction

As said before a FragmentTransaction gives us methods to add, replace, or remove fragments in Android. It gives us an interface for interacting with fragments.


fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);

The method replace(int containerViewId, Fragment fragment) replaces an existing Fragment object from the container containerViewId and adds the the Fragment fragment

fragmentTransaction.addToBackStack(null);

This method, addToBackOfStack(String name), adds this transaction to the back stack, this can be used so that Fragments are remembered and can be used again by the Activity

fragmentTransaction.commit();

The method commit() schedules this transaction, this is not instantaneous; It is scheduled on the main thread to be done when the thread is ready.

Reference



来源:https://stackoverflow.com/questions/29782808/what-does-fragmentmanager-and-fragmenttransaction-exactly-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!