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-
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-
- onAttach():- called when fragment has attached to the activity
- onCreate():- called after attaching the fragment to the host activity
- onCreateView(): -called by the fragment for drawing the user interface
- onViewCreated(): – This method is called after the view has been created by the onCreateView()
- onActivityCreated(): It is used to indicate that the host activity has been created
- onStart():- called when the user interface is visible to the user
- onResume():- called when the app user can interact with the user interface of the application
- onPause(): – This method is called when the user is moving to the next fragment or moving back to another fragment
- onStop(): – called after the onPause() method
- onDestroyView():-It is called when the view created by the onCreateView() method has been destroyed or removed
- onDestroy(): – It is called to completely remove the fragmented state and its resources from the host activity.
- 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.
Table of Contents
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.