Introduction to using fragment in Android Application Development

Fragment is a new concept since Android honeycomb 3.0. Fragment is called fragment, but it is very similar to activity. The function and usage of Android fragment are described below. Fragment is used to describe some behaviors or some user interfaces. In an activity, you can combine multiple fragments, create multiple UI panels in a single activity, and reuse fragments in multiple activities. You can think that fragment is a module in an activity. Fragment has its own life cycle and receives its own input events, You can add or remove activities from the run

A fragment must always be embedded in an activity. At the same time, the life cycle of the fragment is affected by the activity. For example, when the activity is suspended, all fragments in the activity will be destroyed. However, when an activity is running, such as resume, you can manipulate each fragment separately, such as adding or deleting.

As a new feature of Android 3.0, fragment has some powerful functions, such as merging two activities, as shown in the figure below

As shown in the figure above, activity a is used to represent the article title list, and activity B is used to represent the specific content of the article. We can see the layout of two activities combined into one activity through two fragments, which has a good display panel for large screen devices such as tablets. However, because the life cycles of fragments and activities are complex, the following figure shows the life cycle of fragments:

Activity and fragment are compared as follows:

The life cycles of the two are very similar and closely related. To create a fragment, you must create a subclass or existing subclass of the fragment, such as the following code

Oncreate() is called when the fragment is created. You should initialize some practical components, such as those that need to be recovered when the fragment is paused or stopped

Oncreateview() when the system calls fragment to draw the user interface for the first time, if you draw a UI in your fragment, you must return a view. Of course, you can return null, which means that the fragment has no UI

So how to add a fragment to the activity? The layout of an activity can be written like this

Generally, fragments are embedded as part of the host activity UI and the entire view hierarchy of the activity. There are two ways to add a fragment to the activity layout:

1、 Declare the fragment in the layout file of the activity. You can specify the layout attribute for the fragment as for the view (after SDK 3.0). An example is an activity with two fragments:

The Android: name attribute in < fragment > specifies the fragment class instantiated in layout

When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the oncreateview() method on each to obtain the layout of each fragment. The system inserts the view returned from the fragment directly into the location of the < fragment > element

Note: each fragment needs a unique identifier. If the activity is restarted, the system can be used to recover the fragment (and you can also capture the fragment to process transactions, such as removing it.)

There are three ways to provide an identifier for a fragment:

2、 Use the fragment manager to add a fragment to an existing ViewGroup

Whenever an activity runs, you can add fragments to the activity layout. Simply specify a ViewGroup where fragments need to be placed. In order to operate fragment transactions in your activity (such as adding, removing, or replacing a fragment), you must use the API from the fragmenttransaction

You can obtain an instance of fragmenttransaction from your activity as follows:

FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Then you can add a fragment using the add () method, specifying the fragment to be added and the view to be inserted

The first parameter of add () is the ViewGroup where the fragment is to be placed, which is specified by the resource ID. the second parameter is the fragment to be added. Once a change is made with fragmenttransaction, in order to make the change effective, you must call commit ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>