The fragment must always be embedded in an activity, and its life cycle is directly affected by the life cycle of the host activity.

The content of this article can be divided into the following sections:

Using the support library

Create a fragment

Create a dynamic ui

Communication between multiple fragments

In an activity, a fragment represents a behavior or part of the user interface.You can combine multiple fragments to create a multi-panel UI in one activity, and you can reuse a fragment in multiple activities. You can think of a fragment as a module part of an activity,fragment has its own life cycle,Responding to his own input events,And you can add or delete a fragment while the activity is running.

1.Use the support library

If your application needs to be running version 3.0 and above,You can ignore this part.

If your app uses versions below 3.0, 1.6 and above,Need to build using support libraries.

To use the support library:

1. Download the android support package using the SDK Manager tool under SDK

2. Create a libs directory under the top-level directory of your android project

3. Find /extras/android/support/v4/android-support-v4.jar under your SDK and copy it to your project's libs, select this jar package → right-click → build path → add to build path

4. Add thetag in the manifest.xml file of your project:

Android:targetsdkversion="8" />

Where targetsdkversion is the minimum supported version of your software

5. If your project supports versions below 3.0,Please import the following package:android.support.v4. *;

When using fragment activity, please inherit fragment activity instead of activity. If your system is version 3.0 or above,Also need to import similar packages,But ordinary activities can be used.

2.Create a fragment

Fragments support use in different activities and can handle their own input events and life cycle methods.Can be seen as a child activity.

Create a fragment

Creating a fragment is very similar to creating an activity. Inherit the fragment class and override the lifecycle method.The main difference is that you need to override an oncreateview () method to return the layout of this fragment. example:

The life cycle method of the fragment depends on the life cycle of the activity,For example, when an activity's onpause () lifecycle method is called, all fragments' onpause () methods in this activity will also be called.

For more details, please refer to the class fragment.

Add fragment to activity using xml

Although fragments can be reused by multiple activities, you must also associate fragments to a fragment activity. You can use XML layout files to achieve this association.

Note:The above-mentioned fragmentactivity is applicable to API versions below 3.0,Version 3.0 and above can use ordinary activities.


The fragment tag is used above, android:name="" specifies a fragment added to the xml. For more information on creating different screen size layouts,Please read Supporting different screen sizes.

When you add a fragment to a fragment in the layout xml file of an active layout definition,You cannot delete fragments at runtime.If you plan to interact and exchange clips between users,You have to add the active segment when the activity is started for the first time.

3.Build a flexible UI

The fragmentmanager provides operations for adding, deleting, and replacing fragments of the runtime of the activity.

You can add fragments during activity execution instead of defining them in the xml layout file.If you plan to change the life cycle of the fragment in the activity.

If i want to add, delete, modify,You must obtain a fragmenttransaction object through the fragmentmanager object, and perform these operations through its api.

To add a fragment to an activity, you must add the fragment to a container view.example:

In the activity, you can get the fragment object by getfragmentmanager (), and then obtain the fragmenttransaction object by using the fragmenttransaction () method of the fragmentmanager object. Use its add () method to add a fragment to the current activity.

A fragmenttransaction object can perform multiple addition, deletion, and modification methods.If i want to submit these changes to the activity, you must call the object's commit () method at the end. example:

Since it is not defined in the xml layout,All operations that can be deleted and modified.

If you replace or delete a fragment and then allow the user to navigate to the previous fragment, you must call addtobackstack () to add it to the back stack before calling commit ().If you add this fragment to the back stack,After submission, the fragment will be stopped instead of destroyed. If the user navigates to this fragment, the fragment will be restarted instead of re-created.If you did not add it to the back stack,It will be destroyed when it is deleted or replaced. example:

4. Interaction with other fragments

There should be no communication between two separate fragments.They should use their existing activities as a link of communication.

In order to achieve the interaction of two fragments, you can define an interface in the fragment,Then define a method in this interface,The methods in this interface are called in the fragment's onattach () method.Then let the activity implement this method to complete the communication between the activity and the fragment.example:

Define the interface and call the method:

Implement the interface,This method can interact with the data of other fragments:

You can find a fragment by finding fragmentbyid () of the fragmentmanager.

  • Previous A preliminary understanding of JavaScript, Ajax, jQuery, and comparing the three relationships
  • Next Detailed drawing methods using Java's Graphics class