Android Fragments


A fragment is that part of an activity which is responsible for enabling the modular activity design.

Also Read: Android Content Providers

Points to Remember About Fragment
The below list will enlighten a few points about the fragment.

* A fragment follows its own layout and a set of behavior. This is achieved by the help of various lifecycle callback methods.

* It is possible to add or remove any fragment of the activity while the activity is on its run.

* A number of fragments when combined into a single entity, it presents a multi-pane user interface.

* A single fragment can be reused by including it in several different activities.

* The life cycle of a fragment follows the life cycle of its associated activity (or host activity). In other words, if an activity is paused for some reason then the fragments associated with the activity will be stopped.

* A fragment can easily implement a behavior without being associated with any of the UI components.

* Fragments were also a part of Android API version 11 of Android 3.0 (a.k.a Honeycomb version).

A fragment creation requires you to extend the Fragment base class. And the declaration of the fragment should be done under the activity layout file. For the declaration purpose, you will be needed to use the <fragment> tag.

You must know that before the fragments were introduced, the programmers faced a limitation of displaying a single activity at one time. This gave rise to another issue where the user cannot work on two different activities as the screen can’t be divided into two parts. But after the fragments came into existence, the screen division became possible. And thus, it eliminated the issue of performing a single activity at a given point of time. The introduction of fragments made it possible to have a single activity associated with multiple fragments. Even these fragments do possess their own life cycles, layouts, and events associated with them.

Now, take a look at the following example which will explain how two different fragment-defined user interface modules can be embedded in a single activity. This is layout is for a tablet design but the layout will differ for the handset design, as shown in the figure.

As shown in the figure, the Activity A is combining two different fragments, which are Fragment A and Fragment B. This combination is possible for the tablet design. But for the handset design, space is limited for both the fragments and that is why it is not possible for both the fragments to be tied into one single Activity. So, for the handset design, the Activity A will be having Fragment A while the Activity B will be having Fragment B.

Life Cycle of A Fragment
We have mentioned this already that the life cycle of an Android fragment follows the life cycle of its host Android activity. Well, look at the below figure to understand the various stages of the life cycle of a fragment.

Follow the given list of methods which are required to override while you are defining your own fragment class.

* onAttach() – The instance of a fragment always follows the instance of its host activity. This method is used to initialize the activity instance as the activity and the fragment instances are not initialized completely. That is why onAttach() method carries a reference for the activity. This method then helps the fragment to initiate its work.

* onCreate() – The onCreate() method is called whenever the creation of the fragment is required. In here, all the necessary components of the fragment should be initialized first. The components which should be initialized are those which needs to be retained after the fragment is either stopped or paused.

* onCreateView() – This method is called whenever the system is ready to draw the fragment’s user interface for the first time. This method should return a View component so that the fragment’s user interface can be drawn. This returned value will behave as the root of the fragment’s layout. If you don’t want the fragment to provide a UI then you can make it return a null value.

* onActivityCreated() – This callback method is implemented just after the onCreateView() method. In other words, the method is called after the creation of its host activity. The creation of the activity and the fragment instances depend upon the view hierarchy of the activity. findViewById() method can help you to access the view. For an instance, you can instantiate all the objects which are in need of the Context object.

* onStart() – The onStart() method will be initiated just after the Fragment gets visible to the user.

* onResume() – After this method is called, the Fragment will resume its working. This can be possible once the Fragment is paused.

* onPause() – This method indicates that the user is about to stop the fragment. This is the time where you can perform the required changes before the user leaves the fragment.

* onStop() – Once this method is called, the Fragment will stop working.

* onDestroyView() – The Fragment view will be destroyed once the onDestroyView() method is called.

* onDestroy() – This callback method is implemented whenever the final cleanup of the fragment’s final state is required. But it is not necessary that the method will be called by the Android platform.

A Simple Way to Use Fragments
Follow the given step-to-step guide on how to create Fragments.

Step 1: Make sure that how many fragments do you want to combine to build an activity. For an instance, if you want to have two fragments for your activity. One of the fragments will be dedicated to handling the landscape mode while another one is dedicated to handling the portrait mode of the device.

Step 2: Based on this required number of fragments, you need to create as many as classes to extend the Fragment base class. This newly created Fragment class will have the above-mentioned callback methods in it. And as per your requirement, you can override any of the methods provided.

Step 3: Now, for each and every fragment, you are required to create XML layout files. These layout files will be carrying the layouts for the defined fragments.

Step 4: Finally, as per your requirement, make the desired changes in your activity file. Well, in this file, you will be replacing the fragments with the actual logic.

Various Types of Fragments
There are 3 types of fragments available. Take a look at the following table to know more about the types of the fragments.

S. No. Types of Fragments Description
1 Single Frame Fragments * According to this fragment, the view can have only one fragment.
* This type of fragments are used for the handheld devices such as mobile phones.
2 List Fragments * The fragments that are having a special view as a list are referred as list fragments.
3 Fragments Transaction * The fragment transaction is referred to the movement of one fragment to the other.