Many applications are now developed based on nested fragment in fragmentactivity, so if we can clearly know their life cycle, it will make our development easier.
For the life cycle of the activity, I have already described it in detail in the previous article Activity life cycle-based on the most recent summary of experiments, So this article is just from a practical point of view fragment life cycle and fragment and activity life cycle of the corresponding relationship, like most introduction fragment life cycle of the article, first on two graphs:
Okay, here we are.
Here, the relationship between activity and fragment life cycle is illustrated from the perspective of examples.
Assuming our activity is mainactivity, there are two fragment in activity: one called homefragment (the default open page), and one called Userinfofragment
Now let's look at their relationship in a simulated way.
Scenario 1 launches the app for the first time, and activity opens the page by default Homefragment
Scene 2 turn off the power button (the screen is off)
Scene 3 screen unlock
Scenario 4 switch to userinfofragment for the first time
Scenario 5 homefragment and userinfofragment switch back and forth
Here's how to add fragment:
private void Showfragment (String tag) {Fragmentmanager fm = Getsupportfragmentmanager (); Fragmenttransaction ft = fm.begintransaction (); if (! Textutils.isempty (Curfragmenttag)) {Fragment F = fm.findfragmentbytag (Curfragmenttag); if (f! = null) {ft.hide (f);}} Fragment Fragment = Fm.findfragmentbytag (tag), if (Fragment = = null) {Ft.add (r.id.main_layout, getfragmentinstance (tag) , tag);} else {ft.show (fragment);} Ft.commit (); curfragmenttag = tag;}
The
If you change the method of adding fragment from add to replace, each switchover will: (assuming switching from homefragment to Userinfofragment)
In fact, this raises a question:
If you use Replace, each time you switch, fragment will be re-instantiated, reload the data again, which will consume performance and user data, replace is generally in the previous fragment no longer
Used in case of use. If we do not want to instantiate the fragment repeatedly, we can use the Add method, switch to hide the current fragment, and then add another fragment, when you switch again, you only need to hide the current fragment, and then show the other.
Scene 6 Press the home key back to the desktop (assuming the current switch to homefragment)
Scenario 7 Returning from the desktop to the app
Actually, the discovery process is the same as turning off the power key
Scenario 8 exits the app (provided that instances of both homefragment and userinfofragment have been created and are added using the Add method)
Summarize:
Managing the Fragment life cycle is somewhat like managing the activity 's life cycle. Fragment can survive in three different states:
Resumed:
The Fragment is in a running activity and is visible.
Paused:
The other activity is at the top level, but the activity of the fragment is not fully covered (the activity on the top level is translucent or does not occupy the entire screen).
stoped:
Fragment not visible. It is possible that the activity in which it is located is in a stoped state or fragment is deleted and added to the back stack. The fragment of this state still exists in memory.
also similar to activity , you can put fragment The state of the is saved in a bundle , activity is This thing is needed when span style= "line-height:1.5" >recreated . You can save the state in the onsaveinstancestate () method and oncreate ( or oncreateview () or onactivitycreated () Recovery in the .
The biggest difference between Fragment and the life cycle of Activity is the process of storing to the back stack. activity is automatically pressed into the stop stack when stopped, and the stack is managed by the system, and fragment is a back stack that is pressed into Activity and only you are deleting After fragment and explicitly called the Addtobackstack () method is pressed in.
However, the life cycle of management fragment is very similar to the life cycle of managing activity . What you need to think about is how the life cycle of the activity affects the fragment life cycle.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fragment life cycle-the latest experiment-based summary