I am very confused between these functions and their purposes. What I have observed that using replace()
replaces the existing fragment with a new one. We can use addToBackStack(null)
to put that fragment in back stack so we can go back to the previously shown fragment. Now when a fragment is added (or replaced) - onAttach()
-> onCreate()
etc.... methods of the fragment are called in order.
Now when we call remove()
on the fragment from our activity, which functions of the fragment are called and in which order?
What does attach()
and detach()
do? Does detach()
remove the fragment? And when these two attach()
and detach()
are used, which functions of the fragment are called and in which order??
Also, what happens on popBackStack()
?? I mean which functions are called when we use popBackStack()
on the fragment from our activity??
And when does onDestroy() called??
Thank you.
Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order?
Look at http://developer.android.com/reference/android/app/Fragment.html .
The order is: onPause()
, onStop()
, onDestroyView()
, onDestroy()
, onDetach()
What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which functions of the fragment are called and in which order??
attach()
and detach()
is respectively associates or detaches the Fragment
with/from the Activity
. When attaching the Fragment
, the onAttach()
lifecycle method is called, when detaching, the onDetach()
lifecycle method is called in the Fragment
. For more information look at the link above.
Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??
If the Fragment
hasn't been destroyed, then on popBackStack()
the onStart()
and onResume()
methods are called. If the Fragment
has been destroyed previously, then the lifecycle methods will be called starting from onAttach()
. It's the same as, when you press the back button on Activities
.