MyTetra Share
Делитесь знаниями!
Creating a Fragment
Текстовые метки: Fragment
Раздел: Android - Ресурсы - - Training

Creating a Fragment


This lesson teaches you to

  1. Create a Fragment Class
  2. Add a Fragment to an Activity using XML

You should also read

Try it out


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 Fragmentclass using the Support Library so your app remains compatible with devices running system versions as low as Android 1.6.

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 appcompatlibrary, which is compatible with Android 2.1 (API level 7) and also includes theFragment 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:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);

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'sonPause() 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 parentFragmentActivity. 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 regularActivity.

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).


<LinearLayout xmlns:android=""

<fragment android:name=""
android:layout_height="match_parent" />

<fragment android:name=""
android:layout_height="match_parent" />


Tip: For more about creating layouts for different screen sizes, read Supporting Different Screen Sizes.

Then apply the layout to your activity:

import android.os.Bundle;

public class MainActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {

If you're using the v7 appcompat library, your activity should instead extend AppCompatActivity, which is a subclass ofFragmentActivity. For more information, read Adding the App Bar).

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 the next lesson.

MyTetra Share v.0.52
Яндекс индекс цитирования