Fragment Lifecycle in Android
In Android, the fragment is the part of Activity which represents a portion of User Interface(UI) on the screen. It is the modular section of the android activity that is very helpful in creating UI designs that are flexible in nature and auto-adjustable based on the device screen size. The UI flexibility on all devices improves the user experience and adaptability of the application. Fragments can exist only inside an activity as its lifecycle is dependent on the lifecycle of host activity. For example, if the host activity is paused, then all the methods and operations of the fragment related to that activity will stop functioning, thus fragment is also termed as sub-activity. Fragments can be added, removed, or replaced dynamically i.e., while activity is running.
<fragment> tag is used to insert the fragment in an android activity layout. By dividing the activity’s layout multiple fragments can be added in it.
Below is the pictorial representation of fragment interaction with the activity:
Types of Android Fragments
- Single Fragment: Display only one single view on the device screen. This type of fragment is mostly used for mobile phones.
- List Fragment: This Fragment is used to display a list-view from which the user can select the desired sub-activity. The menu drawer of apps like Gmail is the best example of this kind of fragment.
- Fragment Transaction: This kind of fragments supports the transition from one fragment to another at run time. Users can switch between multiple fragments like switching tabs.
Each fragment has it’s own lifecycle but due to the connection with the Activity it belongs to, the fragment lifecycle is infulenced by the activity’s lifecycle.
Methods of the Android Fragment
|onAttach()||The very first method to be called when the fragment has been associated with the activity. This method executes only once during the lifetime of a fragment.|
|onCreate()||This method initializes the fragment by adding all the required attributes and components.|
|onCreateView()||System calls this method to create the user interface of the fragment. The root of the fragment’s layout is returned as the View component by this method to draw the UI.|
|onActivityCreated()||It indicates that the activity has been created in which the fragment exists. View hierarchy of the fragment also instantiated before this function call.|
|onStart()||The system invokes this method to make the fragment visible on the user’s device.|
|onResume()||This method is called to make the visible fragment interactive.|
|onPause()||It indicates that the user is leaving the fragment. System call this method to commit the changes made to the fragment.|
|onStop()||Method to terminate the functioning and visibility of fragment from the user’s screen.|
|onDestroyView()||System calls this method to clean up all kinds of resources as well as view hierarchy associated with the fragment.|
|onDestroy()||It is called to perform the final clean up of fragment’s state and its lifecycle.|
|onDetach()||The system executes this method to disassociate the fragment from its host activity.|
Example of Android Fragment
Fragments are always embedded in Activities i.e., they are added to the layout of activity in which they reside. Multiple fragments can be added to one activity. This task can be carried out in 2 ways:
- Statically: Explicitly mention the fragment in the XML file of the activity. This type of fragment can not be replaced during the run time.
- Dynamically: FragmentManager is used to embed fragments with activities that enable the addition, deletion, or replacement of fragments at run time.
Almost all android app uses dynamic addition of fragments as it improves the user experience. Below is the step-by-step implementation of adding 2 fragments in one activity. A default fragment will be visible when the activity appears on the screen and the user can switch between the 2 fragments at the run time.
Note: Following steps are performed on Android Studio version 4.0
Step 1: Create a new project
- Click on File, then New => New Project.
- Choose Empty activity
- Select language as Java
- Select the minimum SDK as per your need.
Step 2: Modify strings.xml file
All the strings which are used in the activity are listed in this file
Step 3: Working with the activity_main.xml file
Open the activity_main.xml file and add 2 buttons to it which will be used to switch between the 2 fragments. Further, add the fragment element in the activity layout. It is the area in which the fragments will be displayed.
The android:name tag under the <fragment> element is containing the file name of default fragment which is to be displayed when activity opens.
Step 4: Creating the two fragment class
These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.
1. First Fragment class:
2. Second Fragment Class:
Step 5: Creating Layouts for both the fragments
Create two Layout Resource Files for both the fragments. Fragment displays a text on the screen and have a background color to differentiate their area in the Activity layout. Below is the code to implement this layout.
1. fragment_one.xml file:
2. fragment_two.xml file:
Step 6: Working with the MainActivity.java file
Now, the functionality of the button to perform operations on clicking will be defined in the MainActivity class. Moreover, the code for the replacement of fragments during run time is also mentioned in this file. Below is the code to implement this step.