Fragment lifecycle during Fragment transaction

The post covers what all lifecycle’s methods of fragment are normally called during different cases of fragment transaction.It also covers the effect of setting setAllowOptimization() to true on the lifecycle during fragment transaction.The method calls are listed in the sequence in which they are called as per latest appcompat version 25.2.0.Suppose two fragments 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.

Fragment A will only be affected if it’s removed or replaced. Simply adding another fragment will just display fragment B over fragment A and no life cycle callbacks of fragment A should be called.

Then using popBackStack() in Fragment B

Fragment B

onPause

onStop

onDestroyView

onDestroy

onDetach

No lifecycle methods of Fragment A is being called

Since fragment B was added on top of A, fragment A is not affected by the removal of B.

Case 2

When Fragment B replaces Fragment A

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, fragementB).commit();

Fragment B

onAttach

onCreate

Fragment A

onPause

onStop

onDestroyView

onDestroy

onDetach

Fragment B

onCreateView

onActivityCreated

onStart

onResume

When Fragment B replaces Fragment A,Fragment A is destroyed and Fragment B is created.

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

Fragment A

onPause

onStop

onDestroyView

Fragment B

onCreateView

onActivityCreated

onStart

onResume

onDestroy and onDetach method of Fragment A is NOT called.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.

Then using popBackStack() in Fragment B

Fragment B

onPause

onStop

onDestroyView

onDestroy

onDetach

Fragment A

onCreateView

onActivityCreated

onStart

onResume

View of Fragment A is recreated and Fragment B is destroyed

Setting setAllowOptimization() to true

FragmentTransaction class has a method named setAllowOptimization().It sets whether or not to allow optimizing operations within and across transactions.Setting its value to true during fragment transaction,you will see change in the sequence in which lifecycle’s methods are called.

In Case 2 during replacing Fragment, lifecycle methods of Fragment B are called before lifecycle methods of Fragment A if setAllowOptimization() is set to true 

Fragment B

onAttach-onCreate-onCreateView-onActivityCreated-onStart-onResume

Fragment A

onPause-onStop-onDestroyView-onDestroy-onDetach

In Case 3 during replacing fragment,lifecycle methods of Fragment B are called before lifecycle methods of Fragment A if setAllowOptimization() is set to true

Fragment B

onAttach-onCreate-onCreateView-onActivityCreated-onStart-onResume

Fragment A

onPause-onStop-onDestroyView

In Case 3 using popBackStack() in Fragment B,lifecycle methods of Fragment A are called before lifecycle methods of Fragment B if setAllowOptimization() is set to true .

Fragment A

onCreateView-onActivityCreated-onStart-onResume

Fragment B

onPause-onStop-onDestroyView-onDestroy-onDetach

In Case 1, sequence of lifecycle method calls will remain same whether setAllowOptimization is true or false.

Thanks to Hanan Rofe Haim for clearing my doubts regarding fragment transaction in this question !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s