Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Fragment's lifeCycle methods calls during fragment transaction

Tags:

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

like image 501
Android Developer Avatar asked Feb 14 '17 05:02

Android Developer


People also ask

Which of the following lifecycle methods are called during the created state of a fragment?

These include onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . A fragment's view has a separate Lifecycle that is managed independently from that of the fragment's Lifecycle .

How Fragment life cycle is related with activity life cycle?

A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop. Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.


1 Answers

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().

like image 134
Hanan Rofe Haim Avatar answered Oct 10 '22 02:10

Hanan Rofe Haim