Create a fragment

You can think of a fragment as a modular section of an activity, which has its own lifecycle,
receives its own input events, and which you can add or remove while the activity is running (sort
of like a "sub activity" that you can reuse in different activities). This lesson shows how to
extend the Fragment class using the Support Library so your app
remains compatible with devices running system versions as low as Android 1.6.

Instead of setting up your dependent components in the lifecycle methods of your Fragment you
should create a lifecycle-aware component instead. The component can handle any setup or tear down
that is required as your Fragment moves through its lifecycle. The lifecycle-aware component can
then be reused in other Fragments and Activities to avoid duplication of code, and reduce the
amount of setup you need to do in the Fragments/Activities themselves. For more information, read
Handling Lifecycles with Lifecycle-Aware Components.

Before you begin this lesson, you must set up your Android project to use the Support Library.
If you have not used the Support Library before, set up your project to use the v4
library by following the Support Library
Setup document. However, you can also include the app bar in your activities by instead using the
v7 appcompat library, which is compatible with Android 2.1 (API level 7)
and also includes the Fragment APIs.

Create a Fragment Class

To create a fragment, extend the Fragment class, then override
key lifecycle methods to insert your app logic, similar to the way you would with an Activity class.

One difference when creating a Fragment is that you must use the
onCreateView() callback to define the layout.
In fact, this is the only callback you need in order to get a fragment running. For
example, here's a simple fragment that specifies its own layout:

Just like an activity, a fragment should implement other lifecycle callbacks that allow you to
manage its state as it is added or removed from the activity and as the activity transitions
between its lifecycle states. For instance, when the activity's onPause() method is called, any fragments in the activity also receive a call
to onPause().

More information about the fragment lifecycle and callback methods is available in the Fragments developer guide.

Add a Fragment to an Activity using XML

While fragments are reusable, modular UI components, each instance of a Fragment class must be associated with a parent FragmentActivity. You can achieve this association by defining each
fragment within your activity layout XML file.

Note:FragmentActivity is a
special activity provided in the Support Library to handle fragments on system versions older than
API level 11. If the lowest system version you support is API level 11 or higher, then you can use a
regular Activity.

Here is an example layout file that adds two fragments to an activity when the device
screen is considered "large" (specified by the large qualifier in the directory
name).

Note: When you add a fragment to an activity layout by defining
the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan
to swap your fragments in and out during user interaction, you must add the fragment to the activity
when the activity first starts, as shown in
Build a flexible UI.