Basics of Fragment Lifecycle Coordination

jigar mori
3 min readSep 7, 2020

--

In my previous article I have explained a bit about android activity Lifecycle coordination. In this one I am going to share about fragment’s Lifecycle coordination with add and replace. If you haven’t read it till now, you can go and click below

Suppose we have one activity and in this activity we have one fragment with the name FirstFragment so when it’s first load into the activity below Lifecycle event will be called for this fragment

onAttach > onCreate > onCreateView >onViewCreated >onActivityCreated > onStart > onResume of FirstFragment will be called

Now if we have one button in this fragment, And on click of this button it will load another fragment lets call this fragment SecondFragment. In android we can make this transaction using add or replace, Both of the method have different impact on Lifecycle of fragment. We will discuss it one by one.

when user navigate FirstFragment to SecondFragment in case of addtobackstack and replace

onAttach > onCreate of SecondFragment
onPause > onStop > onDestroyView of FirstFragment
onCreateView >onViewCreated >onActivityCreated > onStart > onResume of SecondFragment

Here we have used the addtobackstack, so on back press we can redirect back to the FirstFragment, If we didn't had use the addtobackstack then there will be one little change in above mentioned Lifecycle, onDetach of the FirstFragment will be called directly after the onDestroyView of FirstFragment and before the onCreateView of SecondFragment.

Now if user choose add for the transaction with addtobackstack then lifecycle event will be

onAttach > onCreate > onCreateView >onViewCreated >onActivityCreated > onStart > onResume of SecondFragment

As you can see, none of the FirstFragment Lifecycle event will be called. This is the main difference in add and replce . And if we even don’t add addtobackstack still no change in Lifecycle event calling

Now when user press the back button to go back to the FirstFragment, Lifecycle event call for replace with addtobackstack

onPause > onStop > onDestroyView > onDestroy >onDetach of SecondFragment
onCreateView >onViewCreated >onActivityCreated > onStart > onResume of FirstFragment

Now if there is no addtobackstack then the change is, onDetach of FirstFragment was already called so only all of the SecondFragment Lifecycle event will be called and none from the FirstFragment Lifecycle event from above events, and the activity backpress will be called

Our another backpress scenario is add with addtobackstack lets see the lifecycle event for this

onPause > onStop > onDestroyView > OnDestroy > onDetach of SecondFragment

As you can see only SecondFragment Lifecycle event will be called, as while adding FirstFragment didn't came into onpause so its already there, Now if there is no addtobackstack then what would be Lifecycle event on onbackpress, Instead of writing I think it would be easy for me to share the Screenshot of lifecycle log which explain the Lifecycleevent callbacks

add without addtobackstack and onBackpress

Note: All of the above examples are without animation, If you are using animation then Lifecycle event callbacks will be bit different onDestroyview and afterword lifecycle events (onDestroy and OnDetach) will be called after animation finish if applicable, That’s it for our today's topic, Happy learning!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

jigar mori
jigar mori

No responses yet

Write a response