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();
}
  1. onCreate() Activity
  2. onAttach() Fragment_A
  3. onCreate() Fragment_A
  4. onCreateView() Fragment_A
  5. onActivityCreated() Fragment_A
  6. onStart() Fragment_A
  7. onStart() Activity
  8. onResume() Activity
  9. 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.

  1. onAttach() Fragment_A
  2. onCreate() Fragment_A
  3. onCreateView() Fragment_A
  4. onActivityCreated() Fragment_A
  5. onStart() Fragment_A
  6. 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.

  1. onPause() Fragment_A
  2. onPause() Activity
  3. onStop() Fragment_A
  4. onStop() Activity
  5. onDestroyView() Fragment_A
  6. onDestroy() Fragment_A
  7. onDetach() Fragment_A
  8. onDestroy() Activity

Creation phase of new mode of activity.

  1. onAttach() Fragment_A
  2. onCreate() Fragment_A
  3. onCreate() Activity
  4. onCreateView() Fragment_A
  5. onActivityCreated() Fragment_A
  6. onAttach() Fragment_A
  7. onCreate() Fragment_A
  8. onCreateView() Fragment_A
  9. onActivityCreated() Fragment_A
  10. onStart() Fragment_A
  11. onStart() Fragment_A
  12. onStart() Activity
  13. onResume() Activity
  14. onResume() Fragment_A
  15. 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.

  1. onPause() Fragment_A
  2. onPause() Activity

Now I dismiss the popUp

  1. onResume() Activity
  2. onResume() Fragment_A

Case:4 Pressing the home button or launching another activity.

  1. onPause() Fragment_A
  2. onPause() Activity
  3. onStop() Fragment_A
  4. onStop() Activity

Comming back to application from task manager.

  1. onRestart() Activity
  2. onStart() Fragment_A
  3. onStart() Activity
  4. onResume() Activity
  5. 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.

  1. onPause() Fragment_A
  2. onPause() Activity
  3. onStop() Fragment_A
  4. onStop() Activity
  5. onDestroyView() Fragment_A
  6. onDestroy() Fragment_A
  7. onDetach() Fragment_A
  8. 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.

  1. onPause() Fragment_A
  2. onStop() Fragment_A
  3. onDestroyView() Fragment_A
  4. onDestroy() Fragment_A
  5. 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

  1. onAttach() Fragment_A
  2. onCreate() Fragment_A
  3. onCreateView() Fragment_A
  4. onActivityCreated() Fragment_A
  5. onStart() Fragment_A
  6. onResume() Fragment_A

Fragment_A is having a button that will launch Fragment_B and will have LifeCycle as below:

  1. onAttach() Fragment_B
  2. onCreate() Fragment_B
  3. onCreateView() Fragment_B
  4. onActivityCreated() Fragment_B
  5. onStart() Fragment_B
  6. onResume() Fragment_B

Fragment_B is having a button that will launch Fragment_C

  1. onAttach() Fragment_C
  2. onCreate() Fragment_C
  3. onCreateView() Fragment_C
  4. onActivityCreated() Fragment_C
  5. onStart() Fragment_C
  6. 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:

  1. onPause() Fragment_A
  2. onPause() Fragment_B
  3. onPause() Fragment_C
  4. onPause() Activity
  5. onStop() Fragment_A
  6. onStop() Fragment_B
  7. onStop() Fragment_C
  8. onStop() Activity
  9. onDestroyView() Fragment_A
  10. onDestroy() Fragment_A
  11. onDetach() Fragment_A
  12. onDestroyView() Fragment_B
  13. onDestroy() Fragment_B
  14. onDetach() Fragment_B
  15. onDestroyView() Fragment_C
  16. onDestroy() Fragment_C
  17. onDetach() Fragment_C
  18. 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

  1. onPause() Fragment_C
  2. onStop() Fragment_C
  3. onDestroyView() Fragment_C
  4. onDestroy() Fragment_C
  5. onDetach() Fragment_C
  6. onCreateView() Fragment_A
  7. onActivityCreated() Fragment_A
  8. onStart() Fragment_A
  9. onResume() Fragment_A
  10. onCreateView() Fragment_B
  11. onActivityCreated() Fragment_B
  12. onStart() Fragment_B
  13. onResume() Fragment_B

Currently, B is on top so pressing the back button from Fragment_B will have the following callbacks

  1. onPause() Fragment_B
  2. onStop() Fragment_B
  3. onDestroyView() Fragment_B
  4. onDestroy() Fragment_B
  5. onDetach() Fragment_B

Currently, A is on top so pressing the back button from Fragment_A will have the following callbacks

  1. onPause() Fragment_A
  2. onStop() Fragment_A
  3. onDestroyView() Fragment_A
  4. onDestroy() Fragment_A
  5. onDetach() Fragment_A

Currently, Activity is only available so once on back press destruction of activity will be called.

  1. onPause() Activity
  2. onStop() Activity
  3. onDestroy() Activity

Case:9 Replacing Fragment_B from Fragment_A:

fragmentTransaction.replace(R.id.fram_container, fragment, "Fragment_B");
  1. onAttach() Fragment_B
  2. onCreate() Fragment_B
  3. onPause() Fragment_A
  4. onStop() Fragment_A
  5. onDestroyView() Fragment_A
  6. onCreateView() Fragment_B
  7. onActivityCreated() Fragment_B
  8. onStart() Fragment_B
  9. onResume() Fragment_B

Case:10 Pressing back button from Fragment_B if not added to back stack:

  1. onPause() Fragment_B
  2. onPause() Activity
  3. onStop() Fragment_B
  4. onStop() Activity
  5. onDestroyView() Fragment_B
  6. onDestroy() Fragment_B
  7. onDetach() Fragment_B
  8. onDestroy() Activity

Case:11 Pressing back button from Fragment_B i.e added to back stack (called by replace):

  1. onPause() Fragment_B
  2. onStop() Fragment_B
  3. onDestroyView() Fragment_B
  4. onDestroy() Fragment_B
  5. onDetach() Fragment_B
  6. onCreateView() Fragment_A
  7. onActivityCreated() Fragment_A
  8. onStart() Fragment_A
  9. onResume() Fragment_A

Case:12 Killing the app from Fragment_B (called by replace):

  1. onPause() Fragment_B
  2. onPause() Activity
  3. onStop() Fragment_B
  4. onStop() Activity
  5. onDestroyView() Fragment_B
  6. onDestroy() Fragment_B
  7. onDetach() Fragment_B
  8. 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

--

--

--

Senior Android App Developer | Startup | Product base | Java | Kotlin | MVVM | Architecture components | Android Blogger

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Navigation in Multi-page Apps | Part 2

INTRODUCTION TO ROOM DATABASE

#FlutterWithSakshi -Part 4

Android Background in a nutshell. Part II — Loaders.

Is Kotlin better than Java?

How to provide Backup & Restore Feature for Room Database in Android

Secure your shared preferences with this JetSec library

9 Great Features Qualcomm’s Snapdragon 865 Is Bringing to 2020 Smartphones

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aalishan Ansari

Aalishan Ansari

Senior Android App Developer | Startup | Product base | Java | Kotlin | MVVM | Architecture components | Android Blogger

More from Medium

Android Export and Import Data

Context and Shared Preferences in Android — An Intro

What is Android Architecture?

Android Architecture

A practical guide to Automated Android app testing