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.
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?
This answer is not useful. Show activity on this post. It seems to all depend on whether the fragment is retained or not. When the fragment is retained, then after onDestroyView comes onCreateView.
After onDestroyView() , you can (and likely should) release all of your View references to allow them to be garbage collected. In many cases, it's not necessary, as if it's just happening during a configuration change, onDestroy() will immediately follow and the whole instance will be garbage collected.
A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.
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
Take a look on the diagram:
This is the explicit visualization of all lifecycle states. Enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With