Signup/Sign In

Fragments in Android

A Fragment in Android is a component which can be used over an activity to define an independent modular UI component attached to the activity. It functions independently, but as it is linked to the Activity, when an activity is destroyed, the fragment also gets destroyed.

If you know Biology, and are aware of the concept of Host and Parasite, then in Android, Activity is the host while a Fragment is a parasite.

Fragment has its own lifecycle events, which are different from an Activity's lifecylce events.

An Activity can have any number of fragments in it, although it is suggested to not to use too many fragments in a single activity.

Also, a fragment is a re-usable component, hence, a single fragment can be included in multiple activities, if required.

Generally, fragments are used to create multi-pane UI in Android apps.

A Fragment has it's own Layout for the UI(user interface), but we can even define a fragment without any layout, to implement a behavious which has no user interface, more like a background service.

So, Fragment is a very interesting component of Android OS which can be used in multiple ways in an android app.


Why we need Fragments in Android?

If we already have Activity, and a Fragment is just like a sub-activity, then what is the use of having an additional component in Android?

Well, before the introduction of Fragments(Fragments were added in Honeycomb version of Android i.e API version 11), we could only have a single Activity on a screen at a given point of time, and there was no way to divide the screen and control the different parts separately.

And as the screen size of the Mobile devices are increasing, it makes more sense to show more stuff at the same time on the screen, hence Fragments are very useful, and are very popular amongst the Android developers community.

Android Fragment example


The usage of Fragment in an android app totally depends on the screen size of the device on which the app is being used. If the screen size is big, then we can easily show 2 or maybe more fragments on the screen, but if the display size is smaller, it is advised to use Fragments in separate activities.

Android Fragment example for Tablet and Mobile

Therefore it is also suggested to keep the design of a Fragment modular and independent, so that it can be used on different screens/activity based on the screen size or any other factor.


Main use of Fragments in Android

Following are the 3 main usage of Fragments in Android, for which Fragments were introduced:

  1. Modularity: If a single activity is having too many functional components, its better to divide it into independent fragments, hence making the code more organized and easier to maintain.
  2. Reusability: If we define aany particular feature in a fragment, then that feature more or less becomes a reusable component which can be easily intgerated into any activity.
  3. Adaptability: If we break UI components of an app screen into fragments, then it becomes easier to change their orientation and placement, based on screen size etc.

Fragment Life Cycle

The Fragment lifecycle begins when it is attached to an activity. Below we have shown the complete lifecycle of fragment in form of a flow chart.

Fragment Lifecycle in Android


Lifecycle Methods for Fragment

This is called when the fragment becomes active or interactive.
Method NameDescription
onAttach(Activity)It is called once, when the fragment is attached to the activity.
onCreate(Bundle)The system calls this method when a fragment is created. This is an important method and you should implement the essential components of the fragment in this method.
onCreateView()This method is called when the UI of the fragment has to be initialised. It is called when the fragment is first created and then when the fragment returns back to the layout from the back stack. This method usually returns a View component but if the fragment doesn't have a UI, then you can return a null.
onActivityCreated()This method is called when the host activity is created. By this time, we can even access the fragment's view using the findViewById() method.
onStart()This method is called when the fragment becomes visible on the device's screen.
onResume()
onPause()This is called when a fragemnt is no longer interactive and the user is about to leave the fragment. At this point, it is suggested to save any data for the existing user session, if required.
onStop()This method is called when the fragment is no longer visible.
onDestroyView()This is called when the fragment is to be be destroyed. Here you can do the clean up the resources before the fragment is destroyed.
onDestroy()This is called for the final clean up of fragment's state.
onDetach()It is called just before the fragment is detached from the host activity.

In our next tutorial, we will learn how to implement Fragments in android.