Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

android - What does FragmentManager and FragmentTransaction exactly do?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...