Android Fragments
Fragments are a part of an activity that enable a more modular design of the activity. We can consider fragments as sub-activities.
Here are some important points about fragments:
- Fragments have their own layout, behavior, and lifecycle callbacks.
- You can add or remove fragments in an activity while the activity is running.
- 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 tied to its host activity. This means if the activity is paused, all fragments in the activity are stopped.
- Fragments can implement behavior without user interface components.
- Fragments were added to the Android API in version 11.
Create fragments by inheriting the Fragment class. Before the introduction of fragments, we had a limitation where only a single activity could be displayed on the screen at a given time. We couldn't split the device screen and control different parts independently. With the introduction of fragments, we gained more flexibility and removed the limitation of having only one activity on the screen at a time. Now we can have a single activity composed of multiple fragments, each with its own layout, events, and full lifecycle.
Here is a typical example demonstrating how two UI modules defined by fragments can be combined in an activity designed for tablets and separated in an activity designed for handheld devices.
When running on a tablet-sized device, the application can embed two fragments in Activity A. On a phone screen, where there isn't enough space, Activity A only contains the fragment with the article list. When the user clicks an article, it launches Activity B containing the second fragment to read the article.
Fragment Lifecycle
Android fragments have their own lifecycle, similar to Android activities. Here is a brief introduction to the different stages of its lifecycle.
Here 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. Typically, you get the activity reference here, which will be used in future initialization tasks of the fragment.
- onCreate(): The system calls this method when creating the fragment. You need to initialize essential components of the fragment. These components are needed when the fragment is paused or stopped and should be retained for recovery.
- 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.
- onActivityCreated(): Called after the onCreateView() method when the host activity is created. The activity and fragment instances and the activity's view hierarchy are created. At this point, the views can be accessed via findViewById(). In this method, you can instantiate objects that require a Context object.
- onStart(): Called when the fragment becomes visible.
- onResume(): Called when the fragment becomes interactive.
- onPause(): The system calls this method when the user is about to leave the fragment. Typically, you need to commit any persistent changes here that should persist beyond the user session.
- onStop(): Called when the fragment is about to be stopped.
- onDestroyView(): Called after this method, the fragment is about to 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?
Here are the simple steps to create fragments:
- First, decide how many fragments you need in the activity. For example, we need two fragments to handle the landscape and portrait modes of the device.
- Next, based on the number of fragments, create classes that inherit from the Fragment class. The Fragment class contains the callback functions mentioned above. Override any methods according to your needs.
- For each fragment, create a layout in an XML file. These files contain the defined layout of the fragments.
- Finally, modify the activity file to define the actual fragment replacement logic based on your requirements.
Types of Fragments
Basic fragments can be divided into the following three types:
- Single Frame Fragment - Single frame fragments are used by handheld devices like mobile phones. A fragment is displayed like a video.
- List Fragment - A fragment that contains a special list view is called a list fragment.
- Fragment Transitions - Used with fragment transactions. You can move from one fragment to another.