You can think of a fragment as a modular section of an activity, which has its own lifecycle, es 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 extendFragmentClass Using the support library so your app remains compatible with devices running system versions as old as Android 1.6.
Note:If you decide for other reasons that the minimum API level your app requires is 11 or higher, you don't need to use the support library and can instead use the framework's built in
FragmentClass and related APIs. Just be aware that this lesson is focused on using the APIs from the support library, which use a specific package signature and sometimes slightly different api names than the versions has ded in
The platform.
Create a fragment class
To create a fragment, extendFragmentClass, then override key lifecycle methods to insert your app logic, similar to the way you wowould with
ActivityClass.
One difference when creatingFragmentIs that you must use
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.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, a fragment shocould 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
onPause().
More information about the fragment lifecycle and callback methods is available in
Fragments developer guide.
Add a fragment to an activity using XML
While fragments are reusable, modular UI components, each instance of
FragmentClass must be associated with a parentFragmentActivity. You can achieve this association by defining each fragment within your activity layout XML file.
Note: FragmentActivityIs 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
largeQualifier in the directory name ).
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 more information about creating layouts for different screen sizes, read
Supporting different screen sizes.
Here's how an activity applies 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 a fragment to an activity layout by defining the fragment in the layout XML file, you
CannotRemove 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.