Correlation between Activity lifecycle and Fragment lifecycle

Below are the explanation of activity and fragment lifecycle. Based on some use cases.
Case:0 I am launching activity and adding a fragment in onCreate() of activity so the lifecycle method call stack will be like.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate() Activity");
addFragment_A();
}private void addFragment_A() {
Fragment_A fragment = new Fragment_A();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fram_container,fragment,"Fragment_A");
fragmentTransaction.commit();
}
- onCreate() Activity
- onAttach() Fragment_A
- onCreate() Fragment_A
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onStart() Activity
- onResume() Activity
- onResume() Fragment_A
Case:1 Activity is visible, I have a button in the activity which will add the Fragment_A to frame_layout.
These are the methods will get called.
- onAttach() Fragment_A
- onCreate() Fragment_A
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onResume() Fragment_A
Case:2 Now my activity is having fragment and I am changing the oriantation of activity.
Destruction phase of current mode of activity.
- onPause() Fragment_A
- onPause() Activity
- onStop() Fragment_A
- onStop() Activity
- onDestroyView() Fragment_A
- onDestroy() Fragment_A
- onDetach() Fragment_A
- onDestroy() Activity
Creation phase of new mode of activity.
- onAttach() Fragment_A
- onCreate() Fragment_A
- onCreate() Activity
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onAttach() Fragment_A
- onCreate() Fragment_A
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onStart() Fragment_A
- onStart() Activity
- onResume() Activity
- onResume() Fragment_A
- onResume() Fragment_A
above you can see Fragment_A methods are calling 2 times because we are calling fragment creating in onCreate() of activity.
If you want to prevent this call so do like below:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate() Activity");
if(savedInstanceState==null){
addFragment_A();
}
}
Case:3 Any popUp comes on top of the visible fragment.
- onPause() Fragment_A
- onPause() Activity
Now I dismiss the popUp
- onResume() Activity
- onResume() Fragment_A
Case:4 Pressing the home button or launching another activity.
- onPause() Fragment_A
- onPause() Activity
- onStop() Fragment_A
- onStop() Activity
Comming back to application from task manager.
- onRestart() Activity
- onStart() Fragment_A
- onStart() Activity
- onResume() Activity
- onResume() Fragment_A
Case:4 Now fragment added by a button click or in onCreate() of activity and did not call fragmentTransaction.addToBackStack(“Fragment_A”), And now pressing back button then following method will get a call of activity and fragment respectively.
Note: Because we did not added fragment to back stack so the back press will be considered of activity and activity will be destroyed and fragment is on the activity so fragment will also destroy.
- onPause() Fragment_A
- onPause() Activity
- onStop() Fragment_A
- onStop() Activity
- onDestroyView() Fragment_A
- onDestroy() Fragment_A
- onDetach() Fragment_A
- onDestroy() Activity
Case:5 Now fragment added by a button click and added to back stack by calling fragmentTransaction.addToBackStack(“Fragment_A”), And now pressing back button then following method will get a call of activity and fragment respectively.
Note: Because we added fragment to back stack so the back press will be considered of the fragment so only fragment will be destoryed.
- onPause() Fragment_A
- onStop() Fragment_A
- onDestroyView() Fragment_A
- onDestroy() Fragment_A
- onDetach() Fragment_A
Case:6 I have 3 fragment name as Fragment_A,Fragment_B and Fragment_C now adding all the fragment by calling a button on each fragment like below:
Note: Here all the fragment launched by calling fragmentTransaction.add(R.id.fram_container, fragmentName, “Fragment_Tag”) and not added to backStack.
Added Fragment_A LifeCycle
- onAttach() Fragment_A
- onCreate() Fragment_A
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onResume() Fragment_A
Fragment_A is having a button that will launch Fragment_B and will have LifeCycle as below:
- onAttach() Fragment_B
- onCreate() Fragment_B
- onCreateView() Fragment_B
- onActivityCreated() Fragment_B
- onStart() Fragment_B
- onResume() Fragment_B
Fragment_B is having a button that will launch Fragment_C
- onAttach() Fragment_C
- onCreate() Fragment_C
- onCreateView() Fragment_C
- onActivityCreated() Fragment_C
- onStart() Fragment_C
- onResume() Fragment_C
Now Fragment_C is on top and all other fragments named Fragment_A and Fragment_B are also in on resume so you can interact with all, but look wise they are behind the Fragment_C all are in onResume() state
Case:7 Now I press the back button
Note: I have not added fragment to back stack to back press will considered as activity back press and laying all fragment will be destroyed in following manner:
- onPause() Fragment_A
- onPause() Fragment_B
- onPause() Fragment_C
- onPause() Activity
- onStop() Fragment_A
- onStop() Fragment_B
- onStop() Fragment_C
- onStop() Activity
- onDestroyView() Fragment_A
- onDestroy() Fragment_A
- onDetach() Fragment_A
- onDestroyView() Fragment_B
- onDestroy() Fragment_B
- onDetach() Fragment_B
- onDestroyView() Fragment_C
- onDestroy() Fragment_C
- onDetach() Fragment_C
- onDestroy() Activity
Case:8 Now I added all the fragments by add() method and adding all to the back stack and now pressing the back button.
Currently, C is on top so on pressing back button from Fragment_C will have following call backs
- onPause() Fragment_C
- onStop() Fragment_C
- onDestroyView() Fragment_C
- onDestroy() Fragment_C
- onDetach() Fragment_C
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onResume() Fragment_A
- onCreateView() Fragment_B
- onActivityCreated() Fragment_B
- onStart() Fragment_B
- onResume() Fragment_B
Currently, B is on top so pressing the back button from Fragment_B will have the following callbacks
- onPause() Fragment_B
- onStop() Fragment_B
- onDestroyView() Fragment_B
- onDestroy() Fragment_B
- onDetach() Fragment_B
Currently, A is on top so pressing the back button from Fragment_A will have the following callbacks
- onPause() Fragment_A
- onStop() Fragment_A
- onDestroyView() Fragment_A
- onDestroy() Fragment_A
- onDetach() Fragment_A
Currently, Activity is only available so once on back press destruction of activity will be called.
- onPause() Activity
- onStop() Activity
- onDestroy() Activity
Case:9 Replacing Fragment_B from Fragment_A:
fragmentTransaction.replace(R.id.fram_container, fragment, "Fragment_B");
- onAttach() Fragment_B
- onCreate() Fragment_B
- onPause() Fragment_A
- onStop() Fragment_A
- onDestroyView() Fragment_A
- onCreateView() Fragment_B
- onActivityCreated() Fragment_B
- onStart() Fragment_B
- onResume() Fragment_B
Case:10 Pressing back button from Fragment_B if not added to back stack:
- onPause() Fragment_B
- onPause() Activity
- onStop() Fragment_B
- onStop() Activity
- onDestroyView() Fragment_B
- onDestroy() Fragment_B
- onDetach() Fragment_B
- onDestroy() Activity
Case:11 Pressing back button from Fragment_B i.e added to back stack (called by replace):
- onPause() Fragment_B
- onStop() Fragment_B
- onDestroyView() Fragment_B
- onDestroy() Fragment_B
- onDetach() Fragment_B
- onCreateView() Fragment_A
- onActivityCreated() Fragment_A
- onStart() Fragment_A
- onResume() Fragment_A
Case:12 Killing the app from Fragment_B (called by replace):
- onPause() Fragment_B
- onPause() Activity
- onStop() Fragment_B
- onStop() Activity
- onDestroyView() Fragment_B
- onDestroy() Fragment_B
- onDetach() Fragment_B
- onDestroy() Activity
Case:13 Having 3 fragments in viewPager and changing the page by swipe:

Case:14 Having 3 fragments in viewPager and changing the page by tab click:

Will be adding more cases in the future meanwhile you can play with the code.
https://github.com/Aalishan565/Fragment-LifeCycle-All-Test-Cases-Android