Android Fragment
Android Fragment
A Fragment is a part of an Activity, making the Activity design more modular. We can consider a Fragment as a sub-Activity.
Here are some important knowledge points about Fragments -
- A Fragment has its own layout, its own behavior, and its own lifecycle callbacks.
- When an Activity is running, you can add or remove Fragments in the Activity.
- You can combine multiple Fragments in a single Activity to build a multi-column UI.
- Fragments can be used in multiple Activities.
- The lifecycle of a Fragment is closely associated with its host activity. This means that when the activity is paused, all Fragments in the activity are stopped.
- Fragments can implement behavior without a user interface component.
- Fragments were introduced to the Android API in Android API version 11.
Fragments are created by inheriting the Fragment class. Fragments can be inserted into your activity by declaring them in the layout file of the activity using
Before the introduction of Fragments, we had a limitation because at any given point in time, only a single activity could be displayed on the screen. We could not split the device screen and have independent controls for different parts. With the introduction of Fragments, we gained greater flexibility, and the restriction that only a single activity can be on the screen at any given time was removed. Now we can have a single activity, but each activity is composed of multiple Fragments, and each Fragment has its own layout, events, and complete lifecycle.
Below is a typical example demonstrating how two UI modules defined by Fragments can be combined in an activity designed for a tablet and separated in an activity designed for a handheld device.

When running on a tablet-sized device, this application can embed two Fragments in Activity A. On a mobile device screen, due to insufficient space, Activity A only contains a Fragment with an article list. When the user clicks an article, Activity B, which contains the second Fragment, is launched to read the article.
Fragment Lifecycle
Android Fragments have their own lifecycle, which is similar to Android Activities. Below is a simple introduction to the different stages of its lifecycle.

This is a list of methods you can override in the Fragment class:
- onAttach(): The Fragment instance is associated with the Activity instance. The Fragment and Activity are not fully initialized yet. Typically, you obtain a reference to the Activity in this method, which will be used in the Fragment's future initialization work.
- onCreate(): The system calls this method when the Fragment is created. You need to initialize some necessary components of the Fragment. These components need to be retained when the Fragment is paused or stopped so that it can be restored.
- onCreateView(): The system calls this method when the Fragment is about to draw its user interface for the first time. To draw the Fragment's UI, you need to return a View component representing the Fragment's root layout from this method. If the Fragment does not provide a user interface, return null directly.
- onActivityCreated: This method is called after onCreateView() when the host Activity has been created. The Activity and Fragment instances, along with the Activity's view hierarchy, are created. At this time, the view can be accessed using the findViewById() method. In this method, you can instantiate objects that require a Context object.
- onStart(): This method is called when the Fragment is visible.
- onResume(): Call this method when the fragment is interactive.
- onPause(): The system calls this method when it first indicates that the user is about to leave the fragment. Typically, you need to commit any persistent changes that will outlive the user session here.
- onStop(): Called when the fragment is about to be stopped.
- onDestroyView(): After calling this method, the fragment will be destroyed.
- onDestroy(): This method is used to clean up the fragment's state. However, it is not guaranteed to be called on the Android platform.
How to use fragments?
This introduces the simple steps to create a fragment.
- First, decide how many fragments you need to use in the activity. For example, we need to use two fragments to handle the device's landscape and portrait modes.
- Next, create a class inheriting from class Fragment based on the number of fragments. Class Fragment contains the callback functions mentioned above. Override any method according to your needs.
- For each fragment, you need to create a layout in the XML file. These files contain the layout of the defined fragments.
- Finally, modify the activity file based on the requirements to define the actual fragment replacement logic.
Fragment Types
Basic fragments can be divided into the following three types:
- Single-frame fragment- Single-frame fragments are used on handheld devices such as mobile phones. A fragment is displayed like a video.
- List fragmentFragments that contain a special list view are called list fragments.
- Fragment transitionUsed with fragment transactions. Can move from one fragment to another fragment.