22

Looking at a Fragment's lifecycle, I am not sure about the scenarios that can happen here. There are 2 possible ways to go when a Fragment stops being active.

  1. call the appropriate callbacks, destroy view and then destroy the fragment
  2. call the callbacks, destroy view, but keep the fragment itself alive

Which of the two alternatives is done in which situations? What decides which of them? If a fragment is added to the backstack, then removed/replaced, why not throw it away? Why keep it?

Edit: it dawned on me, could it be dependant on whether the fragment is retained or not?

enter image description here

Martin Melka
  • 5,854
  • 10
  • 60
  • 114
  • change orientation then log in the lifecycle methods and check one yourself one situation. – Raghunandan May 11 '14 at 14:52
  • Related posts - [Activity's onDestroy / Fragment's onDestroyView set Null practices](https://stackoverflow.com/q/26369905/465053), [fragment lifecycle: when “ondestroy” and “ondestroyview” are not called?](https://stackoverflow.com/q/17195641/465053), & [Why implement onDestroy() if it is not guaranteed to be called?](https://stackoverflow.com/q/6117341/465053) – RBT Aug 16 '18 at 06:37

3 Answers3

38

It seems to all depend on whether the fragment is retained or not. When the fragment is retained, then after onDestroyView comes onCreateView.

When the fragment is retained (i.e. setRetainInstance(true)), then the log while rotating the devicelooks like this:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume

But when it is not retained, it goes like this:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroy
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
Martin Melka
  • 5,854
  • 10
  • 60
  • 114
  • Great thanks for the idea of `setRetainInstance(true)`! In my case, it caused `onDestroyView` not calling when activity with fragment was closed. And when reopening that activity `onDestroyView` from old instance was called after `onCreateView` of new instance. – Artem Mostyaev Apr 11 '16 at 14:48
  • @ArtemMostyaev ,hey I need your little help I am also facing the same issue ,onDestroyView from old instance was called after onCreateView of new instance . Could you please suggest me way to solve this – Vivek Nov 23 '20 at 06:24
  • 2
    @Vivek Do not rely too much on `onDestroyView`. It is rarely used as all unused objects can be automatically collected by GC. If you really need it, you can move your code to `onPause` and use `onPause/onResume` pair to save/load state. – Artem Mostyaev Nov 23 '20 at 10:13
12

Take a look on the diagram:

States of Activity, Fragment and Fragment Manager

This is the explicit visualization of all lifecycle states. Enjoy.

Oleksandr Kucherenko
  • 1,771
  • 1
  • 15
  • 20
1

When the fragment is retained (i.e. setRetainInstance(true)),

if setRetainInstance(true) Then :- OnDestroy() is not called and again open fragemnt then onCreate() is not called

but when setRetainInstance(false) :- then fragment all lifecycle is called

a_local_nobody
  • 6,231
  • 5
  • 18
  • 42
Keshav Gera
  • 8,200
  • 1
  • 56
  • 43