A tutorial for creating Fragment components in Android App development

  • 2021-07-10 20:45:11
  • OfStack

You can think of Fragment as a module part of Activity, having its own lifecycle, getting its own events, and you can add or remove it while Activity is running (a bit like a "sub-Activity" that you can reuse in different Activity). This lesson tells you how to use Support Library to inherit the Fragment class, so that your application is still compatible with devices running system versions lower than Android 1.6.

Note: If you decide that the minimum API level required for your application is 11 or higher, you do not need to use Support Library, but can use Fragment within Frameword and related API. Note that this session focuses on API using Support Library, which uses a special package name and is sometimes slightly different from the name of the version API included in the platform.

Before you begin this lesson, you must configure your Android project to use Support Library. If you have not used Support Library before, follow the Support Library Setup documentation and configure your project to use v4 Library. However, you can also include Action Bar in your Activity

Create Fragment
You can think of an Fragment as a module area of an Activity, which has its own lifecycle, receives its own input events, and you can add and delete it while the Activity is running (it's a bit like a child Activity, you can reuse them in different Activity). This lesson shows you how to extend Fragment using the support class library to make your application compatible with older versions like Android 1.6.

Note: If, for one reason or another, you decide that your application needs an API level above 11, then you do not need to use the supporting class library and can use the framework-built Fragment class and the associated API instead. Note that the focus of this lesson is to use API in the support class library, which uses a special package signature, and sometimes the name of API will be slightly different from the version included in the platform.

Create an Fragment class

To create an Fragment, inherit the Fragment class, then override the key lifecycle methods and insert your application logic into it, similar to the Activity class.

One difference when creating an Fragment is that you have to use the onCreateView () callback to define the layout. In fact, in order to get a running Fragment, this is only the 1 callback method you need. For example, the following is a simple Fragment that specifies its own layout:


import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.ViewGroup;

public class ArticleFragment extends Fragment {

  @Override

  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, Fragment should implement other lifecycle callback methods, allowing you to manage its state (add or delete) in Activity and the state of Activity as it transitions between its lifecycle states. For example, when the onPause () method of Activity is called, any Fragment in Activity will also receive a call to the onPause () method.

See the Fragments Development Guide for the Fragment lifecycle and effective callback methods.

Add 1 Fragment to 1 Activity using XML

Fragment is a reusable, modular UI component, and each instance of the Fragment class must be associated with an Activity whose parent class is FragmentActivity. This association is accomplished by defining each Fragment in your Activity layout XML file.

Note: FragmentActivity is a special Activity provided in a supporting class library that handles Fragment prior to API Level 11. If you are using a system version of API Level 11 or higher, you can use a regular Activity.

When the screen is considered large enough, the following layout file adds two Fragment to one Activity (which is placed in the directory name qualified by large).


res/layout-large/news_articles.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="horizontal"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <fragment android:name="com.example.android.fragments.HeadlinesFragment"

       android:id="@+id/headlines_fragment"

       android:layout_weight="1"

       android:layout_width="0dp"

       android:layout_height="match_parent" />

  <fragment android:name="com.example.android.fragments.ArticleFragment"

       android:id="@+id/article_fragment"

       android:layout_weight="2"

       android:layout_width="0dp"

       android:layout_height="match_parent" />

</LinearLayout>

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

The following is an Activity using this layout:


import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.news_articles);

  }

}

Note: When you add an Fragment to an Activity layout by defining an Fragment in the layout XML file, you cannot delete the Fragment at run time. If you intend to switch Fragment during user interaction, you must add Fragment to Activity when Activity is first started.



Related articles: