I am very confused between these functions and their purposes. What I have observed that using
replace() replaces the existing fragment with a new one. We can use
addToBackStack(null) to put that fragment in back stack so we can go back to the previously shown fragment. Now when a fragment is added (or replaced) –
onCreate() etc…. methods of the fragment are called in order.
Now when we call
remove() on the fragment from our activity, which functions of the fragment are called and in which order?
detach() do? Does
detach() remove the fragment? And when these two
detach() are used, which functions of the fragment are called and in which order??
Also, what happens on
popBackStack()?? I mean which functions are called when we use
popBackStack()on the fragment from our activity??
And when does onDestroy() called??
Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order?
The order is:
What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which functions of the fragment are called and in which order??
detach() is respectively associates or detaches the
Fragment with/from the
Activity. When attaching the
onAttach() lifecycle method is called, when detaching, the
onDetach() lifecycle method is called in the
Fragment. For more information look at the link above.
Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??
Fragment hasn’t been destroyed, then on
onResume() methods are called. If the
Fragment has been destroyed previously, then the lifecycle methods will be called starting from
onAttach(). It’s the same as, when you press the back button on
Just a note on
popBackStack(). It doesn’t pop a fragment, it pops a fragment transaction. So whatever the last fragment transaction was is reversed. If you were displaying a
FragmentA and your transaction was:
fragmentTransaction.replace(R.id.your_layout, fragmentB); fragmentTransaction.addToBackStack(null);
It would replace
FragmentB, and add that transaction (not the fragment) to the back stack. If you then hit the back button, it pops the back stack and gets the transaction, which was “replace this
FragmentA with a
FragmentB“. It then reverses that transaction. Backwards, the instruction is to replace whatever the current fragment is with
FragmentA. If the original
FragmentA still exists, it uses that one. If it’s been destroyed, it makes a new one.
Suppose fragment A and fragment B was added to a container with the following steps:
1. Added fragment A => .replace(R.id.container, fragmentA) => addToBackStack(null) 2. Added fragment B => .replace(R.id.container, fragmentB) => addToBackStack(null) 3. Removed fragment B => fragmentManager.popBackStack();
Callbacks when fm.popBackStack() is called:
FragmentB: onPause() FragmentB: onStop() FragmentB: onDestroy() FragmentB: onDetach() FragmentA: onCreateView() FragmentA: onViewCreated() FragmentA: onActivityCreated() FragmentA: onStart() FragmentA: onResume()
Explanation: Why while removing and destroying fragment B using popBackStack(), fragment A view was created again and resumed?
Ans: When you added fragment B, you used replace() and addToBackStack(), so all the fragments were removed from the container, and fragment B was added to the container. And, also this transaction was recorded in the Back stack. So, when fm.popBackStack() is called, first the transaction is popped out from the back stack and so the operations reverted itself, therefore fragment b is removed from the container and destroyed. And all other fragments get added, for our case fragment A’s view is added to the container. Also noted that fragment A’s onAttach & onCreate() is not called because it has already been created & attached to the MainActivity earlier.
Back press on fragment B does the same thing:
If you press the back button, it calls fm.popBackStack() and pops the transaction.