Understanding methods of calling the fragment life cycle during a fragment transaction

I created a demo to understand which fragment life cycle methods are invoked in different cases of a fragment transaction. Although most of the calls are waiting for a few things that I still confuse that I wrote in Bold.

Suppose that there are two fragments A and B, and we perform a 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

Fragment A life cycle methods are not used.

What did I expect?

the onStop method of fragment A is called because fragment A is not displayed

According to the documentation -

Stopped - the fragment is not displayed. Either the host activity was stopped or the fragment was removed from the activity, but added to the rear stop. The stopped fragment is still alive (all state and member information is saved by the system). However, it is no longer visible to the user and will be killed if killed.

Does this mean that no method of the current fragment is called when a new fragment is added to one activity?

Then using popBackStack() in fragment B

Fragment B

Onpause

Onstop

onDestroyView

Ondestroy

onDetach

No fragment A life cycle methods are called

What did I expect?

the onStart method of fragment A is called because fragment A is now displayed

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 expected

Case 3

When fragment B replaces fragment A while holding 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 the onDetach method of fragment A are NOT called. Why is it not called? Bcoz according to the documentation method replace removes all fragments that are already in the container and adds your new to the same container

Then using popBackStack() in fragment B

Fragment A

onCreateView

onActivityCreated

Onstart

onResume

Fragment B

Onpause

Onstop

onDestroyView

Ondestroy

onDetach

+6
source share
2 answers

Does this mean that no method of the current fragment is called when a new fragment is added to one activity?

That's right, your first fragment A will be affected only if it is removed or replaced (case 2). Just adding another fragment will simply display fragment B over fragment A, and lifecycle callbacks should not be called.

What did I expect?

the onStart method of fragment A is called because fragment A is now displayed

Again, since fragment B was added over A, fragment A is independent of the removal of B.

onDestroy and the onDetach method for fragment A are NOT called. Why is it not called? Bcoz, in accordance with the workflow method, replaces all fragments that are already in the container, and adds your new one to the same container

Unlike a simple replacement, when you add a replacement transaction to the back, you actually save the first fragment attached to it, only its representation is destroyed.

Once you have placed the backstack B fragment, it will be deleted, and the A fragment simply recreates its representation - starting with onCreateView ().

+6
source

setUserVisibleHint will be called when the fragment is visible or invisible

0
source

Source: https://habr.com/ru/post/1014954/


All Articles