Fragment lifecycle while doing .add and .replace

Vinod Pattanshetti
4 min readApr 19, 2020

--

As we know, A Fragment represents a behavior or a portion of the user interface in a FragmentActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.

This article is about the lifecycle of the fragment while adding and replacing the fragments to the frame layout containers of an Activity. Before starting the fragment we go through fragment lifecycle once. Following diagram has life cycle methods starting from onAttach() to onDetach().

Fragment life cycle
  1. Fragment .add Transaction

When you add any fragment to the container then it goes through some life cycle methods. Following is the code for .add fragment transaction with addToBackStack(null).

private void addFragment() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fl_container, BlankFragment.newInstance());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}

The significance of the API addToBackStack(null) is when you use this API then fragment transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

For example, there is a Fragment A to the activity’s frame layout container then Fragment A goes through life cycle methods from onAttach() to onResume(), when you add Fragment B on top of Fragment A on same container then Fragment B goes through life cycle methods from onAttach() to onResume() same happens when you put Fragment C on top of Fragment B on the same container.

.add fragment transaction of Fragment A, Fragment B, and Fragment C

In short when you add a fragment then it calls life cycle methods from onAttach() to onResume().

When you click back press button on an android device then fragment C, fragment B, and Fragment A go through some life cycle methods like below.

Back press button on an android device for Fragment add a transaction.

In short when you press the back button then all fragments go through onPause() to onDetach() life cycle methods. The fragment is detached and popped off the stack.

2) Fragment .replace Transaction

When you replace one fragment with another fragment, with the following code which has .replace fragment transaction with addToBackStack(null).

private void replaceFragment() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_container, BlankFragment.newInstance());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}

Let’s say you are replacing Fragment A with Fragment B then first Fragment B’s life methods onAttach() and onCreate() gets called then Fragment A’s life cycle methods get called from onPause() to onDestroyView(), here Fragment A won’t be detached from the stack it is remembered by the stack.

Let’s check the below example in which Fragment B is replacing Fragment A and after that Fragment C is replacing Fragment B.

.replace of Fragment A, then .replace of fragment B and then .replace of fragment C

Let’s check what happens when we click back press button

When you click back press button on an android device then fragment C, fragment B, and Fragment A go through some life cycle methods like below.

back press clicks on fragment C, fragment B, and fragment A respectively.

So, when you click back press button on an android device then first Fragment C goes through onPause() to onDetach() with Fragment B’s onCreateView() to onResume(), please do check the sequence first Fragment C will get called then Fragment B. On second back press click Fragment B goes through onPause() to onDetach() with Fragment A’s onCreateView() to onResume(). On third back press click Fragment A goes through onPause() to onDetach() and activity on which fragments were loaded that activity will be finished.

In short when you press the back button then all fragments go through onPause() to onDetach() life cycle methods. The fragments are detached and popped off the stack.

Important note when you use addToBackStack() API then only fragment transactions will be remembered to the stack after their commit(), if you don’t use addToBackStack() API then fragments won’t be remembered to the stack.

That’s all on the Fragment lifecycle while doing .add and .replace transactions.

--

--

Vinod Pattanshetti
Vinod Pattanshetti

Written by Vinod Pattanshetti

Senior Software Engineer(Android Developer)

Responses (6)