Martin Melka
Martin Melka

Reputation: 7799

When would Fragment's onDestroyView be called, but it wouldn't be destroyed?

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

Upvotes: 36

Views: 35871

Answers (3)

Keshav Gera
Keshav Gera

Reputation: 11254

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

Upvotes: 2

Oleksandr Kucherenko
Oleksandr Kucherenko

Reputation: 2039

Take a look on the diagram:

States of Activity, Fragment and Fragment Manager

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

Upvotes: 14

Martin Melka
Martin Melka

Reputation: 7799

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

Upvotes: 41

Related Questions