I created a demo to understand which all fragment lifecycle's methods are called during different cases of fragment transaction.While most of the calls are as per expectation few things I am still confused which I have written in Bold.
Suppose two fragment A and B are there and we are performing transaction between them
Case 1
When Fragment B is added to Fragment A
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, fragementB).addToBackStack(null).commit();
Fragment B
onAttach
onCreate
onCreateView
onActivityCreated
onStart
onResume
No lifecycle methods of Fragment A is being called.
What i expected was?
onStop method of Fragment A is called since Fragment A is not visible
As per documentation-
Stopped- The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.
Does this mean that no method of current fragment is called when new fragment is added in same activity?
Then using popBackStack()
in Fragment B
Fragment B
onPause
onStop
onDestroyView
onDestroy
onDetach
No lifecycle methods of Fragment A is being called
What i expected was?
onStart method of Fragment A is called since Fragment A is visible now
Case 2
When Fragment B replaces Fragment A
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).commit();
Fragment B
onAttach
onCreate
onCreateView
onActivityCreated
onStart
onResume
Fragment A
onPause
onStop
onDestroyView
onDestroy
onDetach
Everything was as per expectation
Case 3
When Fragment B replaces Fragment A keeping it in backstack
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).addToBackStack("tag").commit();
Fragment B
onAttach
onCreate
onCreateView
onActivityCreated
onStart
onResume
Fragment A
onPause
onStop
onDestroyView
onDestroy and onDetach method of Fragment A is NOT called.Why its not called?Bcoz as per documentation method replace
removes any fragments that are already in the container and add your new one to the same container
Then using popBackStack()
in Fragment B
Fragment A
onCreateView
onActivityCreated
onStart
onResume
Fragment B
onPause
onStop
onDestroyView
onDestroy
onDetach
Does this mean that no method of current fragment is called when new fragment is added in same activity?
Correct, your first fragment A will only be affected if it's removed or replaced (case 2). Simply adding another fragment will just display fragment B over fragment A and no life cycle callbacks should be called.
What i expected was?
onStart method of Fragment A is called since Fragment A is visible now
Again, since fragment B was added on top of A, fragment A is not affected by the removal of B.
onDestroy and onDetach method of Fragment A is NOT called.Why its not called?Bcoz as per documentation method replace removes any fragments that are already in the container and add your new one to the same container
Unlike a simple replace, when you add your replace transaction to the backstack you're actually keeping the first fragment attached to it's activity, only its view is destroyed.
Once you pop the backstack fragment B is removed and fragment A will just recreate its view - starting from onCreateView().