Detailed explanation of the interaction between fragment and activity based on Android

Today, we will continue to explain the characteristics of fragment components, mainly related to the interaction and life cycle of activities. As we said earlier, fragment depends on activities, and the life cycle is also bound to activities. Let's look at the relationship between fragment and activity. 1. Create an event callback method for the activity. In some cases, you may need a fragment to share the event with the activity. A good approach is to define a callback interface in the fragment and ask the host activity to implement it. When the activity receives a callback through the interface, it can share information with other fragments in the layout if necessary. For example, if a new application has two fragments C in the activity, one is used to display the article list (fragment A) and the other is used to display the article content (fragment B) C, then fragment a must tell the activity when a list item is selected, and then it can tell fragment B to display the article.

In this example, onarticleselectedlistener interface declares in fragment A:

Any item that is subsequently added to the option menu from the fragment is appended to the existing menu item. When a menu item is selected, the fragment will also receive a callback to onoptionsitemselected(). You can also register a view in your fragment layout by calling registerforcontextmenu() to provide an environment menu. When the user opens the environment menu, the fragment receives a call to oncreatecontextmenu(). When the user selects an item, the fragment receives a call to oncontextitemselected().

Note: although your fragment will receive the callback after each menu item it adds is selected, in fact, when the user selects a menu item, the activity will first receive the corresponding callback. If the activity's on item selected callback function implementation does not handle the selected item, then the event will be passed to the fragment callback.

This rule applies to the options menu and the environment menu. 3. Handling the lifecycle of fragments managing the lifecycle of fragments is similar to managing the lifecycle of activities in most places. Like activities, fragments can be in three states: resumed. Fragments are visible in running activities. Another activity paid is in the foreground and has focus, but the activity where the fragment is located is still visible (the foreground activity is partially transparent or does not cover the whole screen). Stopped either the host activity has been stopped or the fragment has been removed from the activity but added to the background stack. The fragment in the stopped state is still alive (all state and member information is maintained by the system). However, it is no longer visible to the user, and if the activity is killed, it will also be killed. The corresponding diagram is as follows:

Like an activity, you can use a bundle to maintain the state of the fragment in case the process of the activity is killed and you need to restore the state of the fragment when the activity is recreated. You can save the state during onsaveinstancestate() of the fragment and restore it during oncreate(), oncreateview() or onactivitycreated().

In terms of life cycle, the most important difference between activity and fragment is how they are stored in its background stack. By default, after the activity is stopped, it will be placed in a system managed background stack for saving the activity. (therefore, the user can navigate back to it using the back key).

However, only when you remove a fragment during a transaction and explicitly call addtobackstack () to save the instance is it placed on a background stack managed by the host activity.

In addition, the lifecycle of managing fragments is very similar to the lifecycle of managing activities. Therefore, the same practice in "managing the activity lifecycle" also applies to fragments. What you need to understand is how the life of activity affects the life of fragment.

4. Coordination with activity life cycle the life cycle of the activity in which the fragment exists directly affects the life cycle of the fragment. The callback behavior of each activity life cycle will cause similar callbacks in each fragment.

For example, when an activity receives onpause (), each fragment in the activity receives onpause ().

Fragment has some additional lifecycle callback methods that handle the only interaction with the activity in order to perform actions such as creating and destroying the fragment's UI. These additional callback methods are:

• onattach() is called when the fragment is bound to the activity (the activity will be passed in) • oncreateview() is called when the view hierarchy associated with the fragment is created • onactivitycreated() is called when the oncreate() method of the activity returns • ondestroyview() is called when the view hierarchy associated with the fragment is being removed • ondetach() When a fragment is disassociated from an activity, the process that invokes the fragment lifecycle and the impact of the host activity on it are shown in Figure 3. In this figure, you can see how each state of the activity in turn determines the callback method that the fragment may receive. For example, when an activity receives its oncreate(), fragments in the activity receive at most onactivitycreated(). Once the activity reaches the resumed state, you are free to add and remove fragments in the activity. Therefore, the lifecycle of a fragment can change independently only when the activity is in the resumed state. In any case, when the activity leaves the resumed state, the fragment is pushed into its own life cycle process again by the activity.

5. To summarize the relevant knowledge of fragment, let's stop here for the time being. For example, you can directly see the program in apidemo. If you don't know where the API demo is, please Baidu! Learning programming needs to learn to find answers by yourself.

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