What I already known is:
after fragmentTransaction.replace()
, current fragment's onStop()
function will be called while fragmentTransaction.add()
won't.
and after calling fragMgr.popBackStack();
, we will return to previous fragment no matter fragmentTransaction.replace
or fragmentTransaction.add()
is used
So what does fragmentTransaction.replace
do?
I can understand we can "add" a fragment opon a previous fragment and later return to previous fragment by popBackStack()
, BUT:
if previous fragment is "replaced" by current fragment, I guess previous fragment is removed and current fragment is added in, how can it return to previous fragment when popBackStack()
called?
add() is used for simply adding a fragment to some root element. replace() behaves similarly but at first it removes previous fragments and then adds next fragment.
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.
The title of the toolbar should change between the two fragments (from "First Fragment" to "Second Fragment"). The second fragment should have a "back button" (arrow left icon) in the toolbar that, on click, lets the user go back to the first fragment.
You can add multiple fragments to a container and they will be layered one on top of the other. If your fragments have transparent backgrounds you will see this effect and will be able to interact with the multiple fragments at the same time.
This is what will happen if you use FragmentTransaction.add on a container. Your added fragment will be placed on top of your existing fragment.
If you use FragmentTransaction.replace(R.id.container,fragment)
it will remove any fragments that are already in the container and add your new one to the same container.
You can also use the add method without a container id and your fragment will simply be added to the list of fragments in the FragmentManager and you can recall these at any time by their Tag value.
You can still return to a previous configuration IF you added the transaction to back stack. You can do this even if a previous operation removed a fragment. The removed fragment is remembered in the transaction and popping the back stack brings it back.
Let's say you have a fragment container.
And, your task is to add a fragment into the container.
You can do this by calling any of the following methods
1) add(containerId,fragment)
2) replace(containerId,fragment)
Although both methods will add your fragment into the fragment container, their innards(internal working) differ based on the two possible states of the fragment container.
When fragment container
1) does not have any fragment in it.
2) already have one or multiple fragments attached to it.
Let's see what happens when we call add()
and replace()
method.
In this case, both methods will add the fragment to the container. So they will produce same effect.
add()
: adds the new fragment on the top another fragmentreplace()
: removes everything then adds the new fragment
Example
So suppose the Fragment container has fragments[A->B->C]
.
Now you want to add a new fragment D
.add()
method result will be [A->B->C->D]
replace()
method result will be [D]
Check this Demo project for better understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With