Android — Fragments and their Lifecycle.

H Dev
4 min readJul 12, 2021

In order to understand the lifecycle of Fragments, a few basic topics to understand/ grasp the concept of Fragments.

Firstly : What is a fragment?

A fragment is a part or a portion of the user interface in an activity. A fragment aids in making activities into a more modular design.

Unlike activities, fragments cannot exist on their own. They must be hosted/ be a part of an activity(s) or can a be a sub of another fragment as well. One activity can have multiple fragments and a fragment can be a part of multiple activities as well.

How to create a fragment ?

One can create a fragment(s) by extending the Fragment class.

It is also possible to insert a fragment into an activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element.

Also we can use multiple instances of the same fragment class within the same activities or in multiple activities or even as a child/sub of another fragment.

Why do we need fragments?

Fragments can be useful to layout the screen. Without fragments, a screen could only contain an activity. It cannot contain sub-activities hence limiting the possibility of having various segments in an single screen (activity).

Therefore with the presence of fragments, we can have a single activity on a screen but each activity can consist of multiple fragments which will have their own layout, events and complete life cycle, which together comprise a activity by forming its layout.

Fragments can determine the layout, below are two pictures with different layouts but belong to the same activity. This is due to the fact that the same fragments can be structured differently in an activity.

Below mentioned are pictures of an youtube video streaming activity in two different resolutions.

Fragment Lifecycle :

Source : https://developer.android.com/guide/fragments/lifecycle

  • Although each fragment has its own lifecycle, each fragment’s had to be linked another fragment or another activity, therefore it’s lifecycle is affected by it’s parent lifecycle. Therefore even before the lifecycle of the fragment starts, it is linked with the parent activity/fragment ie: On Attach(), the fragment that is going to be created is attached to its parent.

There are five stages in the Fragment lifecycle :

  1. INITIALIZED
  2. CREATED
  3. STARTED
  4. RESUMED
  5. DESTROYED
  • Next, the fragment is created during On Create() callback. By then the On Attach() has been already called. Only after the view is state declared as CREATED state.
  • That is, with YouTube as an example, only after the layout loads are the respective fragments get created in their respective positions. In case of view not equal to null, the state of the fragment is transitioned to STARTED, that is the OnStart() callback is called upon.
  • Now that all the fragments have appeared on the screen, they are now available for user interaction. Ie: they can be clicked upon, scrolled through ect.. This is where the state RESUMED is reached and the OnResume() callback is called upon.
  • Now let’s assume that the user pauses the video, slides the video down and meanwhile browses through other, in such a case, this particular fragment is put on pause and the other fragments are being used. In such cases the fragment (video player) is transitioned to ON_PAUSE state. The fragment invokes Onpause() callback.
  • When the fragment is not visible that means the ON_STOP event occurred and the fragment is moved to the CREATED state. For example, if the video is swiped aside and the video is no longer visible as a thumbnail in the bottom, the state of that fragment has now transitioned due to OnStop() callback. The fragment s replaced in the stack and it can be returned to. In case of Netflix or such streaming apps, the state is saved. You can always resume from where we stopped even after we stop the fragment. But in the case of youtube, this state isnt saved, and when we select the same video again, the fragment is created again and the video starts to play from the start.
  • Finally, the last state is DESTROYED, where the fragment is, as the name suggests, destroyed. This happens when the fragment is removed or the parent activity/ fragment is destroyed. It reached a DESTROY state, an ON_DESTROY even is sent and the OnDestroy() callback is invoked.

Well hoping this provides a brief understanding about Android Fragments and their lifecycle.

--

--

H Dev

just another X-shaped personality, love to learn and tinker with new tech.