Impact of using fragment manager on fragment lifecycle

The life cycle of fragment is normally used in the activity. The first start process is onatach() - oncreate() - oncreateview() - onviewcreated() - onactivitycreated() - onstart() - onresume(); With the activity being retired and destroyed, the declaration cycle of fragment is onpause () - onstop () - ondestroyview () - ondestroy () - ondetach ();

If you use eventbus and other reflection operations in the fragment, the reflection related methods will be called directly after the fragment executes oncreate(). Since the view has not been created by oncreateview() and other methods, null pointer exceptions will occur if you directly update the UI layer in the reflection related methods. This bug led me to focus on the principle of fragment manager.

The following is the usage scenario of a bug: create afragment and bfragment and a corresponding two buttons in an activity. Click abutton to display afragment, and click BButton to display bfragment. At the same time, after performing corresponding operations in bfragment, use eventbus to send an event, and then receive the event in activity and afragment respectively, And perform corresponding operations to display the interface to afragement and update the data in afragement. Because the activity uses the fragment manager to switch between two fragments, there are various problems when affragment receives events. The original unrepaired code is as follows:

There are two ways to switch layouts using fragmentmanager in activity: one is layout replacement, i.e. fragmentmanager. Replace() related methods; the other is layout explicit and implicit, i.e. fragmentmanager. Hide() and fragmentmanager. Show(). In the original code, replace () is used to directly replace the fragment. This method causes that each replaced fragment is re executed from onattach (). Therefore, when the event is sent in the bffragment before, the current affragment has not been created, and the processing operation of the event will not be performed. There are two solutions.

Scheme 1: use the poststicky() method of eventbus to delay sending the event. When receiving the event in afragment, the annotation is sticky = true, and the default is false. So the update code is as follows

Using the log of scheme 1, it is found that the event has been normally received and processed in afragment, but after the event is processed, a series of operations such as updating the view are performed from oncreateview(), resulting in the updated view of event being covered by the updated view. If this problem is solved, as long as the view is not updated in a hurry in handlerevent, Instead, create a new global event to store the current event, judge the global event in onviewcreated(), and update the data if it is not empty. The final modification code of scheme I is as follows:

Scheme 2: use the explicit and implicit method of fragmentmanager instead of the replacement method to switch fragments. When using hide() and show(), you must first call add() to add fragments to fragmentmanager. At this time, switching two fragments will not re execute a series of processes such as oncreate() -- onresume(). Only setuservisiblehint() calling fragments is displayed in the activity Method represents the explicit and implicit of the current fragment, indicating that the fragment has been bound to the activity, and its life cycle can only be saved in onresume() of the activity. The modified code of scheme 2 is as follows:

The above two solutions have their own advantages and disadvantages. Generally speaking, if you want to update the view every time the fragment is displayed, it is better to use scheme 1. However, if scheme 1 is switched multiple times, there is no reference because the previous fragment has been unbound from the current activity, but it still exists in memory. If the system does not release memory in time, There will be a hidden danger of memory leakage; Similarly, for scheme 2, the interface will not be updated every time the switch is made. Therefore, if you want to perform some operations during the display and hiding, you can only display and call the setuservisiblehint () method of fragment and rewrite the method to perform operations. When it comes to the form of scheme 2, it is somewhat the same as the related switching mode of viewpager, which can be used for reference.

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