Fragments in Android

Fragments are like sub-activities which are objects that represent parts of an activity’s UI. Fragment is a modular section of an activity, which has its own lifecycle, receives its own input events. Multiple fragments can be combined to form a single activity to build multipane UI and reuse it as a single fragment in multiple activities. Fragment’s lifecycle is directly affected by the host activity’s lifecycle. If the host activity stops, all the fragments in it are stopped and when the host activity is destroyed, all fragments in to are destroyed. Fragments make it easier to adapt an application to different screen orientations and multiple screen sizes.

  • You must always embed a fragment in an activity. They can only exist within the context of an activity.
  • Fragments define their own layout, their own behaviour and their own life cycle.
  • You can insert a fragment into an activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element or You can add and remove fragments dynamically in an activity at runtime.
  • You can use fragments without its own UI as an invisible worker for the activity.
  • Inorder to acheive reusability, Fragment-to-Fragment communication should not be done. That is we should avoid direct manipulation of one fragment from another fragment.

Fragments introduced in Android 3.0( API level 11)

Android introduced fragments in Android 3.0. If you are using Android 3.0 or higher, then you are all set. You have access to the following,

  • Fragment class, which is the base class for all fragment definitions.
  • FragmentManager- which is the class for interacting with fragment objects inside the host Activity.
  • FragmentTransaction-which is the class for performing fragment transactions like as add, remove, or replace a fragment.

Android Compatibility Library(ACL)

Since Fragments were introduced for Android 3.0 and higher, Google has released the Android Compatibility Library(ACL) for Android 1.6 and higher. The ACL gives you access to the fragment system. Since ACL supports Android versions back to Android 1.6, Android 1.5 devices will not be able to use fragment system. If you are trying to build application using ACL on devices with Android 3.0 or higher, your application uses the class definitions from the Compatibility Package but not the device’s native implementation of the class. For more details on ACL, please refer to the Google documentation V4 library. When using compatibility library, you will need to subbclass FragmentActivity for any Activity that wants to make use of Fragment.

Why to use Fragments

Android runs on variety of devices like phones, tablets and large screen TVs. A Tablet has a large screen than does a phone. A TV has a larger screen than does a tablet. Fragments helps us to handle mutiple screen sizes. Lets assume we have an activity that displays a list of jobs(list of jobs) and details of each job if the user taps on any job in the list.
A tablet is more like a desktop, which has larger screen sizes. In this case, you can have the list of emails and the details of the selected email displaying on the same screen.

When it comes to phones whose screen size is small compared to Tablets and TV’s you cannot have the list and details displaying on the same screen.


you can develop two fragment one for managing the list and another for managing the details of the list for this scenario, depending upon the screen size and change in the layout configurations the Fragments would be resued.

As you can see the picture used for tablets, the Activity A contains the Fragment A and Fragment B being displayed on the same screen. Fragment B updates when the selection is done on Fragment A. Whereas in Handsets because of its small size, the Activity A contains Fragment A which has list of emails, selecting an email in activity A, fires up a new activity named Activity B which has Fragment B displaying the details of the seletced email. This is how fragments helps in reusability while using devices of different size.

Fragments can also address the issue of orientation change. During the orientation change, Android re-creates your activity before switching to new orientation, wherein you have to save the current state of the activity and restore the state once the activity has been recreated. Using fragments you can retain a part of the UI which need not be altered, across orientation changes.

Fragment LifeCycle

Subclasses of Fragment class

Below are the subclasses of a Fragmnt Class

  • ListFragment
  • DialogFragment
  • PreferenceFragment
  • WebViewFragment

Creating a Fragment Class

To define a new fragment, you need to extend the Fragment class( or one of the above mentioned subclasses of Fragment class. The below code shows how to define a fragment,

public class FragmentListActivity extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.listlayout, container, false);

The above code will inflate the view R.layout.listlayout, and add it as a child view to the ViewGroup container. R.layout.listlayout is a simple layout file for your fragment class that you must have created in the path res/layout folder.

To provide a layout for the fragment, you must implement the onCreateview() callback method which returns a view(root of your fragment’s layout). The Android system invokes this method when it is time to put the Fragment on the screen. The arguments passed in to this callback include,

  1. layoutInflater which you can used to inflate a layout for this fragment.
  2. a ViewGroup parent
  3. Saved bundle

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!