30

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?

Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
Wood
  • 945
  • 1
  • 9
  • 18

2 Answers2

56

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.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • 1
    will fragment in this case always be created or existing instance of it will be used? For example: We have Fragment_1, Fragment_2, Fragment_3 and we call transaction.replace(container, Fragment_X) several times in this order: 1, 2, 3, 1, 3, 2, 1 etc... Will all occurrences of Fragment_1 be THE same or all of them will be recreated whenever we use them in transaction.replace(...)? 10q – Ewoks Mar 29 '14 at 11:36
  • 7
    If you do this `replace(R.id.container, new Fragment1())` in order 1, 2, 3, 1, 3, 2, 1. Then there is 7 fragment created and they are different instance. Since you used "replace" instead of "add", only one fragment (the third Fragment1) will be in memory and others are destroyed. Then when you pop the fragment stack, the system will recreate your second Fragment2 (with Arguments and savedInstanceStates of Fragment2). – John Pang Nov 22 '14 at 07:04
29

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.

Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
  • So do you mean that when Fragment D is "add"ed, clicking on Fragment D will propagate the touch to the fragments behind if it has transparent background? – Harsha Oct 03 '21 at 22:43