In this post, we want to discuss the fragment lifecycle and its uses in Android. Before we get started, if you want to know about the divide screen size as a percentage, please go through the following article: Properly divide activity screen as a percentage.
A Fragment is a piece of an activity that enables more modular activity design. The fragment is usually used as part of an activity’s user interface and contributes its own layout to the activity. It will not be wrong if we say, a fragment is a kind of sub-activity.
Following are important points about fragment
- A fragment has its own layout and its own behavior with its own life cycle 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-plane UI.
- A fragment can be used in multiple activities.
- The fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.
- A fragment can implement behavior that has no user interface component.
- Fragments were added to the Android API in the Honeycomb version of Android which API version 11.
Methods that you can override in fragment class
- onAttach(): The fragment instance is associated with an activity instance. Typically, you get in this method a reference to the activity which uses the fragment for further initialization work.
- onCreate(): The system calls this method when creating the fragment. You should initialize the essential components of the fragment.
- onCreateView(): The system calls this method 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.
- onActivityCreated(): The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instances have been created as well as the view hierarchy of the activity.
- onStart(): The onStart() method is called once the fragment gets visible.
- onResume(): Fragment becomes active.
- onPause(): The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.
- onStop(): Fragment going to be stopped by calling onStop()
- onDestroyView(): Fragment view will destroy after calling this method.
- onDestroy(): onDestroy() called to do the final clean up of the fragment’s state but Not guaranteed to be called by the Android platform.
Uses of Fragments
This involves a number of simple steps to create Fragments.
- First of all, decide how many fragments you want to use in an activity. For example, let’s want to use two fragments to handle the landscape and portrait modes of the device.
- Next based on the number of fragments, create classes that will extend the Fragment The Fragment class has the above-mentioned callback functions. You can override any of the functions based on your requirements.
- Corresponding to each fragment, you will need to create layout files in the XML files. These files will have the layout for the defined fragments.
- Finally, modify the activity file to define the actual logic of replacing fragments based on your requirement.