Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between fragmentTransaction.add and fragmentTransaction.replace

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?

like image 859
Wood Avatar asked Dec 19 '13 12:12

Wood


People also ask

What is the difference between replace and add in fragment?

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.

Which method is called when fragment is replaced?

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();

Why do you need to call addToBackStack () before commit transaction?

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.

When replacing a fragment with another one how do you know that the user can get back to the last fragment when they press 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.


2 Answers

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.

like image 122
Kuffs Avatar answered Sep 28 '22 02:09

Kuffs


Two choices

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)

But both methods differ in behavior !!!

enter image description here 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.

Case 1: When there is no fragment attached in a container

In this case, both methods will add the fragment to the container. So they will produce same effect.

Case 2: When the fragmentContainer already has fragment/fragments

add(): adds the new fragment on the top another fragment
replace(): 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]

Relevant Link:

Check this Demo project for better understanding.

like image 21
Rohit Singh Avatar answered Sep 28 '22 04:09

Rohit Singh