Android Fragment

The fragment is the must one that every Android developer must know inside out in order to able to develop a robust Android app. This post will walk through us a few things about Android fragment.

Let's start from what a fragment is, what and how it does, why do we need them and how can we develop ...

What is a fragment?

A Fragment represents a behaviour or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. (from Fragments | Android official documentation)

What and How a fragment does?

We can think of a fragment as a sub-activity and work a lot like an activity. Like an activity, a fragment can have its own view hierarchy and a life cycle. It can even respond to the Back button like activities do.
It is possible to put multiple activities together without using fragments. However, fragments framework provides several features to make saving and restoring fragments much simpler than activities do.

Why do we need Fragments?

The android application is the series of activities grouped together to achieve a larger purpose. For example, an activity might allow users to browse a list of music albums while another one for details for each album. This is fine for smaller screen mobile devices. But for the big screen (10inches or larger), it would be waste of screen space if only one activity is shown, wouldn't it? With fragments, an application can show a list of music albums and show the currently selected album in a detail view at the same time.

Another reason is that we all want to have separate layouts for the portrait case as well as the landscape case. We will need to re-define all layouts (including labels and fields and images, ...ahh...) and provide code for them repetitively. But thanks to fragments, we have a way to group these view objects together and consolidate logic for them. So, the application is divided into a number of chunks that could be reused across different screen sizes, devices and orientation.

When to use Fragments?

In large screens

A Fragment is primarily used to make a chunk of the application's user interface and functionality reusable across devices and screen sizes, especially when developing for tablets. But fragments should be also used for a smaller screens for consistency and reusability of code although there is no room on the screen for showing multiple fragments at the same time.

In small screens

When we use fragments on a device with a smaller screen, either fragment 1 or fragment 2 has to show but not both at the same time. By using fragments in these scenarios, it lets our layouts stay simple as fragments are treated as containers in activity layouts. So, the activity layouts do not need to specify the internal structure of the each fragment. Each fragment owns its own layout for its internal content that are intended to be reused with little configurations.

In screen orientation

Every time the screen rotates, the activity has to be recreated itself by doing a ton of works: such as saving the current state and restoring it once the activity is recreated. Fragments provides chunks that could be easily retained across orientation changes.

In the same activity

Suppose a user is in the main activity and doing blah..blah. And the user interface has changed within the same activity and the user wants to go back a step or two or three. When we are using the old activity style, every time the user presses the Back button will take the current activity out entirely and recreate an another new activity. However, we can create a stack of fragments that can be stepped backward with Back button while letting the user stays inside the current activity.

In smooth transaction

When we got a number of chunks in an activity's user interface by using fragments, we can control the look of transaction between screen smooth, like a polished application.

I did not say coding part is easy, but understanding what to code and why is more difficult.

SO, before we look into how can we develop fragments, we need to take a look at its structure and life cycle for long term knowledge.

How can we develop?

The structure

A Fragment is not an extension of Activity

As we discussed before, a fragment is like a sub-activity. But a fragment is not an extension of Activity and is extended from Object in package, where an activity is subclassed from Context.

A Fragment can have a view hierarchy

The view hierarchy that a fragment has is not different from any other view hierarchy, which can be recreated (inflated) by code or in XML layout specification. The fragment's view hierarchy needs to be attached to the view hierarchy of the surrounding activity because a fragment cannot exist without the context of an activity. Mostly, it is about views sugar-coated with 'Fragment' tag.

A Fragment has two bundles.

First bundle for initialisation arguments

This bundle works as similar as in an activity. A fragment uses these bundle arguments to be saved and restored by the system, like activities. By default, the default constructor (with no arguments) is called when the system restores a fragment. Then restoring becomes with this bundle of arguments to the newly created fragment.

Second bundle for states

A fragment can save state into a bundle object when re-created, and this bundle object is passed through a series of callbacks, onCreate(), onInflate(), onCreateView(), onActivityCreated().

The two bundles are not same as the first one only save the values that should be used to initialise the fragment, not the current state of it.

A Fragment has its manager for back stack

More than one fragments can play in parallel in an activity. When a fragment has been switched out with another fragment, the fragment-switching transaction can be saved on a back stack, which is managed by the fragment manager. The Back button behaviour is managed by this back stack.


  • Fragments can only exist within the context of an activity. So, you can't use a fragment without an activity.
  • Ensure there is a default constructor for a fragment class.
  • Add a bundle of arguments as soon as a new fragment is created. Then other subsequent callbacks can properly works as intended.
  • From a fragment, we can know which activity it is tied to.
  • Through that, we can get the known activity's resources and its fragment manager.

Original banner art from - Designed by Freepik

Nay Win Myint

A JavaScript full-stack web developer, also interested in Android application development and Graphic design.

United Kingdom