Explain in detail the use of fragment in Android Development

Foreword: I have studied Java and Android for nearly a year. During this period, I should have completed an Android client independently and ensured its stability in the main line version. During this period, I encountered many pits and learned a lot of Android knowledge with my senior brother. But people always have to embrace change and can't make themselves too comfortable. Although I don't give up, I have proved my learning ability. The next step is to start doing ROM porting. Here is a summary of the most used fragments in previous projects.

Fragment introduction fragment can be understood as a behavior or part of the user interface in an activity. It must be nested in the activity. However, a fragment has its own independent XML layout file and has good encapsulation, so it can be easily replaced with activity in special cases.

Creating a fragment is similar to creating an activity. You need to implement XML layout files and Java classes. XML layout files are the same as other layout files, such as the following layout file (fragment_layout. XML):

In Java code, the following life cycle methods of fragment can be implemented as needed: 1. Onattach(): when fragment is attached to an activity, it is called, and the activity handle can be obtained in this method to realize the communication between fragment and activity. 2. Oncreate(): initialize the fragment. 3. Oncreateview(): this method will be called when drawing the user interface for the fragment for the first time. 4. Onactivitycreated(): called after the host activity oncreate function is executed. Fragment's own widget instantiation and business logic processing can be performed in this method. 5. Ondestoryview(): called when fragments begin to be destroyed. 6. Onstart(): called when the fragment is visible. There are many other callback functions used to manipulate various stages of the fragment life cycle, which you can learn by yourself.

Fragment lifecycle each fragment has its own set of lifecycle callback methods and processes its own user input events. The corresponding life cycle is shown in the following figure:

To add fragments to an activity, first of all, you need to ensure that acitivity supports fragments. Therefore, an activity usually needs to inherit from fragmentactivity. There are usually two ways to add fragments to an activity: static and dynamic. The static method directly adds a fragment to the XML layout file of the activity, as shown below:

The Android: name attribute in < fragment > specifies the fragment class instantiated in the layout. When the system creates an activity layout, it instantiates each fragment specified in the layout file and calls oncreateview() function for them to obtain the layout of each fragment. The system directly inserts the view returned by the fragment at the < fragment > element. Note: each fragment needs a unique ID. if the activity is restarted, the system can be used to recover the fragment (and capture the transaction processing of the fragment, such as removal). There are three ways to provide ID for fragment:

Dynamic methods use fragmenttransmission. You can use the fragmenttransmission API to operate on the fragments of an activity (for example, add, remove, or replace fragments). Reference codes are as follows:

The first parameter passed into the add () function is the ViewGroup where the fragment is placed, which is specified by the resource ID. the second parameter is the fragment to be added. Once you make a change through fragmenttransmission, you should use commit () to see the change take effect.

Fragments communication fragments should not communicate directly, and their interaction should be carried out through the host activity. There are three ways to interact with fragment and activity: 1. Activity creates a fragment with parameters. 2. The object handle of the fragment is maintained in the activity, and the public method of the fragment can be called directly through the handle. 3. Fragment can obtain the defined listener handle in onattach function. Create a fragment with parameters. In some specific cases, a fragment may require specific parameters to initialize. Since a fragment must have only one parameterless constructor, consider using the static newinstance method to create a fragment with parameters. The example code is as follows:

You can simply load a fragment with parameters in the activity:

Call the fragment method. Because the activity can get the handle of the embedded fragment, it can call the method directly through the fragment handle.

In the activity, this method can be called directly through the object handle of the fragment:

Fragment listener this method is needed if fragment needs to share events with activity. An interface is defined in the fragment and implemented by the activity. Get the activity that implements this interface in the onattach () method. The interface code defined in the fragment is as follows:

Implement this interface in activity:

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
分享
二维码
< <上一篇
下一篇>>