Android Fragments and its Lifecycle
Welcome, you had successfully implemented apps using activity in our previous chapters. Here, we are going to start with the most important topic — Fragments.
To understand what a fragment is, we can think fragment is like a sub-activity. A whole activity which we can see on our screen and if we only need to modify a small part, how we can do that with any modification in activity? So, we need a fragment.
Fragment provides us with 2 major things:
- Modularity
- Adaptability
Modularity
For example — we used Gmail application in our mobile phones, and also in tablets. How is the UI different? Depending on the screen size, it shows us like the image below.
The part in the tablet where the details and the list is showing, both are fragments running on a single activity. On a single activity, multiple fragments can be added and remove.
Adaptability
We can also take one example of WhatsApp, to understand it better. We see three tabs on WhatsApp, swiping which will give us another tab open up. Chat/Status/Calls — these three are the fragments which change in that particular area.
There are some points about fragments:
- A fragment has its layout and its behaviour with its lifecycle callbacks.
- You can add or remove fragments in the activity. Activity is running, which is in the resumed lifecycle state.
- You can combine multiple fragments in a single activity to build a multi-pane UI.
- A fragment can be reuse in multiple activities.
- A fragment life cycle is closely related to the lifecycle of its host activity which means when the activity is in the pause state, all the fragments available in the activity will also stop.
- Fragments added to the Android API in Android 3.0 which API version 11 to support flexible UI on large screens.
The fragment has its lifecycle which runs under the activity lifecycle.
When fragment come up on the screen:-
- onAttach() — This method called first, To know that our fragment has been attached to an activity. We are passing the Activity that will host our fragment.
- onCreate() — This method called when a fragment instance initializes, just after the onAttach where fragment attaches to the host activity.
- onCreateView() — The method called when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.
- onActivityCreated() — This method called when Activity completes its onCreate() method
- onStart() — This method called when a fragment is visible.
- onResume() — This method called when a fragment is visible and allowing the user to interact with it. Fragment resumes only after activity resumes.
When fragment goes out off the screen:-
- onPause() — This method called when a fragment is not allowing the user to interact; the fragment will get change with other fragment or it gets removed from activity or fragment’s activity called a pause.
- onStop() — This method called when the fragment is no longer visible; the fragment will get change with other fragment or it gets removed from activity or fragment’s activity called stop.
- onDestroyView() — This method called when the view and related resources created in onCreateView() are removed from the activity’s view hierarchy and destroyed.
- onDestroy() — This method called when the fragment does its final clean up.
- onDetach() — This method called when the fragment is detached from its host activity.
This is all about how the fragment come up within the activity and goes out. Cool!! We had learnt about the Fragments.