Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 514
Alireza.Heidari Avatar asked Apr 21 '15 20:04

Alireza.Heidari


People also ask

What is the difference between FragmentManager and Supportfragmentmanager?

FragmentManager is class provided by the framework which is used to create transactions for adding, removing or replacing fragments. getSupportFragmentManager is associated with Activity consider it as a FragmentManager for your Activity .

What does addToBackStack null do?

addToBackStack("fragName") : if you want later popToBackStack(String name, int flags) to pop more than one back stack. Use . addToBackStack(null) : If you don't want later pop more than one back stack, but still want to pop one at a time.

How do fragment transactions work?

Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class. You can group multiple actions into a single transaction—for example, a transaction can add or replace multiple fragments.


1 Answers

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

like image 122
Zelldon Avatar answered Sep 28 '22 04:09

Zelldon