Fragment Lifecycle in Android
In this article, all the crucial aspects of Fragment Lifecycle in Android will be explained. This includes everything, ranging from the basics of Fragment in Android to methods used for Fragment Lifecycle in Android. For this reason, we have divided this article in the following sections:
- Fragment in Android
- Examples of Fragments in Android
- Types of Fragments in Android
- Fragment Lifecycle in Android
Fragment in Android
In Android app development, many tools are used to add features in an app to provide the user a smooth and convenient experience. As the user navigates through the app, many of these features go through a number of states one after the other. In other words, it can be said that those features have set lifecycles.
The term “lifecycle” is one of the most recurring words in Android development. If you are studying Android app development, you will come across this term a lot times. In order to create a well-developed Android app, it is important to study lifecycles of many things, which includes activity, service, view, application and fragment. That being said, the main focus of this article would be on Fragment Lifecycle in Android.
Fragments are a useful and powerful tool in Android development. They were introduced in the Android API with the Honeycomb version, which is API level 11. With the help of the fragment class, an Android app developer can create dynamic UIs (User Interfaces). In Android, a Fragment is a part of an Activity that is used to make the activity design more modular. Therefore, it is easier to understand fragment by thinking of it as a kind of sub-activity. In Android development, a fragment is added with the help of the element.
Although it is a part of an activity, a fragment has its own lifecycle and input events. A number of fragments can be added in a single activity to build a multi-pane UI. Conversely, a single fragment can be used in a number of activities of an app. In either case, it is necessary that a fragment must always be embedded in an activity, and, for this reason, the Fragment Lifecycle heavily depends upon the lifecycle of its embedding activity. The Activity Lifecycle and Fragment Lifecycle are so closely linked together that all fragments in an activity get stopped when the host activity is paused.
It is not mandatory for an app to have fragments, but many apps make use of them to modify their appearances, to make it easier to adjust a single app design in various devices of different sizes and improve the flexibility of the UI. Speaking of UI, it is also possible to add fragments in an Android app without having any UI.
Because fragments are closely linked with device size, it is important to study their application in differently sized devices. For example, an app opened in a tablet can show two fragments in a single activity simultaneously; however, the same app opened in a smart phone will have to show the two fragments in two different activities because of the reduced screen size.
Examples of Fragments in Android
Being such a helpful tool for Android app developers and users both, fragments are found in nearly every application. As an example, consider the WhatsApp application. When you open the WhatsApp application in your smart phone, you see three fragments near the top of the first activity. The three fragments are titled as “CHATS,” “STATUS” and “CALLS,” and when you swipe your screen, you open a fragment out of the three.
For the second example, consider opening the Gmail application on a tablet in the landscape mode. After opening the Gmail app, the first activity that appears on your screen shows a list of emails. When you select one of the mails, it opens on the right side of the screen by reducing the width of the list. The box showing the details of the selected mail on the right side and the list of emails on the left side are both fragments, and, in this case, both the fragments appear on a single activity simultaneously because of the larger screen size.
Types of Fragments in Android
To fully understand Fragment Lifecycle in Android, one must know what fragment is, and, for that, studying fragment types is very important. Basically, all types of fragments are created in the same way, but the way they are used is different. In a fragment, there are two parts: UI and code.
You can either create the UI of the fragment either by coding or inflating the XML layout file without changing the behavior of the fragment. However, after defining the fragment, you must choose which type fragment you wish to add. In Android, there are two main types of fragments:
- Static Fragment
- Dynamic Fragment
A static fragment is added within an activity layout, and this fragment never changes. To create a static fragment, you must include it in your XML file by entering the fragment tag within a second XML layout.
On the other hand, a dynamic fragment is added through java coding by using FragmentManager. Therefore, it is not associated with the fragment tag. Unlike static fragment, dynamic fragment is more flexible. Due to this, a user is able to change, add and remove each fragment in runtime. Dynamic Fragments have two main sub-types:
- Framework Fragment
- Support Fragment
Fragment Lifecycle in Android
As mentioned above, each fragment has its own Fragment Lifecycle, and these lifecycles are directly related to the embedding activity lifecycle. Even the most experienced Android app developers get puzzled when it comes to Fragment Lifecycle in Android. So it is important for Android developers to be careful while studying the Fragment Lifecycle. The following diagram illustrates a common Fragment Lifecycle in Android:
While managing a Fragment Lifecycle, a developer first adds the fragment in his project. For that, the onAttach() method is first used because it lets it known that the fragment has been attached to an activity and which activity the fragment has been attached to.
After that, the onCreate() method is called right after the onAttach() method when the fragment is created and when the fragment instance initializes.
In the next step, the onCreateView() method is called in order to make the fragment draw its UI. In order to draw a UI in the fragment, it is important that a View component is returned from this method, which is the root of the layout of the fragment. However, if the fragment is not supposed to have any UI, it can be returned empty or null.
The next stage sees the calling of the onActivityCreated() method. With this method, it is indicated that the host activity or the embedding activity is created. In other words, this method indicates that the onCreate() method of the host activity has taken place.
When everything is created, the onStart() method is called. This method indicates that the fragment is visible because it is called right after the fragment gets visible.
Now that the fragment is visible, a user can interact with it. Therefore, at this stage, the onResume() method is called to resume the fragment and to make it functional for the user. It is important to know that a fragment only resumes after its activity has been resumed.
A resumed fragment can only go towards going off the screen.
For this path, the resumed fragment first becomes invisible to the user. At this stage, the onPause() method is called, which signifies that the user is leaving the fragment. This happens when a fragment is removed from the activity, added to the back stack, replaced by a new fragment and other similar cases.
Afterwards, the onStop() method is called when the fragment is going to be stopped. As this happens when the fragment is no longer visible, this method is always called after the onPause() method. A fragment is stopped after it is paused for the reasons mentioned above or when the host activity is stopped.
Subsequently, whatever we created on the fragment by using the onCreateView() method will be reversed in the stopped fragment. The onDestroyView() method is called when the view and other related resources are removed and destroyed from the view hierarchy of the activity.
If the fragment is waiting on the back stack after being paused and the user now wants it back to the layout, the onCreateView() method is called once again after the onDestroyView() method.
If the onCreateView() method is not called then the onDeathView() method is followed by the calling of the onDestroy() method finally. With this method, the fragment goes through its final clean-up; however, it is not certain that the Android platform would call it.
Then the fragment is disassociated and detached from its embedding activity. To notify this event, the onDetach() method is called after the onDestroy() method. After this, the fragment is completely destroyed.
And that was everything that an app developer needs to know about Fragment Lifecycle in Android. We hope you got an understanding of the basics of Android Fragment and Fragment Lifecycle that is firm and clear enough for you to continue your research. Also, we hope that you now understand the use of fragments and how important they can be for a smart app design.