Introduction to Android Fragment and its Lifecycle

Aditya Tripathi Mar 14th, 2023

Image Source

Fragment is a reusable component of the application user interface in Android. It is also known as the sub-activity. The fragment lifecycle in android is dependent on the host or parent activity that inflates it. Fragment has its own XML and java file which is used to handle user input events. Fragment is completely dependent on the activity. A fragment cannot live on its own, they have to be attached to the host’s view hierarchy. We can add, remove or replace the fragment dynamically within the activity. The fragment is fully dependent on the activity, and thus its lifecycle is also dependent on the activity.
The image here explains how two UI modules i.e. defined by fragments can be combined into one activity for a tablet design but separated for a normal handset design-

the streaming solution

Image Source

ott software

Image Source

When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen. The steps involved in the Fragment lifecycle covers the following-

  1. onAttach():- called when fragment has attached to the activity
  2. onCreate():- called after attaching the fragment to the host activity
  3. onCreateView(): -called by the fragment for drawing the user interface
  4. onViewCreated(): – This method is called after the view has been created by the onCreateView()
  5. onActivityCreated(): It is used to indicate that the host activity has been created
  6. onStart():- called when the user interface is visible to the user
  7. onResume():- called when the app user can interact with the user interface of the application
  8. onPause(): – This method is called when the user is moving to the next fragment or moving back to another fragment
  9. onStop(): – called after the onPause() method
  10. onDestroyView():-It is called when the view created by the onCreateView() method has been destroyed or removed
  11. onDestroy(): – It is called to completely remove the fragmented state and its resources from the host activity.
  12. onDetach():-It is called after the onDestroy() method to notify that the fragment within the host activity has been detached.

If you want to have a lot of UI components, you will probably get hundreds or even thousands of widgets but this problem will occur from time to time in bigger enterprise projects like OTT apps or E-commerce. Fortunately, Fragment supports us to load modules dynamically.

States of Fragment Lifecycle

The Fragment lifecycle begins when it is attached to an activity. Once attached, there are three states of the fragment lifecycle, as given below-

  • Active (or resumed)
  • Paused
  • Stopped

ott platform provider

Image Source

A Detailed Overview of Fragment Lifecycle in Android

onAttach()

A fragment is Fully dependent on the activity and without the activity, we cannot launch our fragment. It is used to let us know that our fragment has been attached to the activity. Please find the code snippet for the OnAttach() method which we can use within our application.

best vod platform

onCreate()

It is used to create the instance of the fragment which we have to display within our activity. After the onAttach() method after attaching the fragment to the host activity,onCreate() is Called. Please find the code snippet for the onCreate() method we can use within our application.

ott service provider

onCreateView()

It is used for the View component to get the UI design from our XML and return it to a fragment to provide the UI for our fragment. Please find the code snippet for the onCreateView() method which we can use within our application.

ott streaming platform

onViewCreated()

It is called when the view has been created by the onCreateView() method. When we have to display a recyclerview and configure the adapter of the recycler view before it is visible to the user, this method is used. Please find the code snippet for the onViewCreated() method we can use within our application.

premium video on demand

onActivityCreated()

It is used to indicate that the host or parent activity has been created in which we are displaying our fragments. Please find the code snippet for the onActivityCreated() method which we can use within our application.

commercial streaming services

onStart()

This method is called when the user interface is visible to the user. Please find the code snippet for the onStart() method we can use within our application.

ott app development

onResume()

It is called when the app user can interact with the user interface of the application. Please find the code snippet for the OnResume() method which we can use within our application.

ott video platform

onPause()

It is called when the user moves to Another fragment or back to another fragment. This method is called when we are doing the transition or removal of the fragment from the host activity. Please find the code snippet for the onPause() method we can use within our application.

video on demand solution

onStop()

After the onPause() method, onStop() is called. This method is called when the fragment is removed from the host activity or the host activity has been stopped. Please find the code snippet for the onStop() method we can use within our application.

ott video on demand

onDestroyView()

When the view created by the onCreateView() method has been destroyed then onDistroyView() is called. Please find the code snippet for the onDistroyView() method which we can use within our application.

video streaming solutions

onDestroy()

It is called to completely remove the fragment state and its resources from the host activity. Please find the code snippet for the onDestroy() method which we can use within our application.

video streaming solution

onDetach()

It is called after the onDestroy() method to notify that the fragment within the host activity has been detached. Please find the code snippet for the onDetach() method which we can use within our application.

ott business

Conclusion

A Fragment represents a reusable portion of your app’s UI, and thus all the steps involved in the lifecycle must be executed well. In fact, fragments still provide a construct for reusing UI, breaking up monolithic activities, and working within the Android lifecycle. Today, a lot of the bugs surrounding Fragment Transactions and Child Fragments have been fixed. And now, android developers can make Fragments smarter by moving business code into testable structures, such as presenters. If you have any queries related to Fragment in Android, do connect with us.

Aditya Tripathi- Android Developer

Add a Comment

Your email address will not be published. Required fields are marked *

Looking for Streaming Solutions?

  • Go-Live on 12+ platforms
  • Zero Revenue Share
  • Multiple Monetization Models
  • 50+ Integrations

get a demo today

Take control of your digital media strategy.
Contact us for a no-obligation demo of the Experience
Cloud, tailor-made for you!