Detailed life cycle of Activity and Fragment

Activity life cycle


Through the above diagram, we can summarize the life cycle of Activity as follows:

1. Start the Activity: The system will first call the onCreate method, then the onStart method, and finally onResume, and the Activity will enter the running state.
2. The current Activity is covered by another Activity or the screen is locked: the system will call the onPause method to suspend the execution of the current Activity.
3. The current Activity returns to the foreground or unlocks the screen from the covered state: the system will call the onResume method to enter the running state again.
4. The current Activity goes to the new Activity interface or press the Home button to return to the main screen, and retreats to the background: the system will call the onPause method first, and then call the onStop method to enter the stagnant state.
5. After the user returns to this Activity: the system will first call the onRestart method, then the onStart method, and finally the onResume method, and enter the running state again.
6. The current Activity is in the covered state or the background is invisible, that is, in steps 2 and 4, the system memory is insufficient, kill the current Activity, and then the user returns to the current Activity: call the onCreate method, onStart method, and onResume method again to enter Operating status.
7. The user exits the current Activity: The system calls the onPause method first, then the onStop method, and finally the onDestory method to end the current Activity.

Note that onPause is different from onStop. OnPause indicates that the Activity is visible but cannot get the user focus. For example, if a dialog box pops up on the current Activity, the user focus is obtained by the dialog box, but the current Activity can still be seen. OnStop indicates that the Activity is in an invisible state, such as jumping from one Activity to another Activity, the previous Activity is in an invisible state.

Fragment life cycle

Because Fragment must be embedded in Acitivity, the life cycle of Fragment is closely related to the Activity in which it is located.
If the Activity is in a paused state, all Fragments in it are in a paused state;
If the Activity is in the stopped state, all Fragments in this Activity cannot be started;
If the Activity is destroyed, all Fragments in it will be destroyed.

When the Activity is in the active state, the state of the Fragment can be independently controlled, such as adding or removing the Fragment. 
When doing fragment transaction (conversion) in this way, the fragment can be put into the back stack of the Activity, so that the user can perform the return operation.


From the above figure, we can see that the life cycle of Fragment and Activity is very similar. Let's take a look at a few methods that only exist in Fragment:

onAttach method: As the name suggests, it is called when Fragment and Activity are associated.
onCreateView method: Called when the layout is loaded for the Fragment.
onActivityCreated method: Called when the onCreate method in the Activity is executed.
onDestroyView method: Called when the layout in the Fragment is removed.
onDetach method: As the name suggests, it is called when Fragment and Activity are disassociated.

Pay attention to onActivityCreated(), because this method is called after the onCreate method in the Activity is executed, so the onCreate of the Activity may not be executed before the onActivityCreated() call, so the UI related to the Activity cannot be performed in onCreateView(). Activity-related UI operations should be performed in onActivityCreated(), while only UI display operations are performed in onCreateView.

In addition, it should be noted that there is no onRestart() method in Fragment, this method only exists in Activity.

Whenever a Fragment is created, first add the following three callback methods:
  • onCreate(): The system calls this method when the Fragment is created. Here, the related components should be initialized, and some things that need to be preserved even if they are suspended or stopped. 
  • onCreateView(): The system calls this method when the Fragment's UI is drawn for the first time. This method will return a View, or null if the Fragment does not provide UI. Note that if inherited from ListFragment, the default implementation of onCreateView() will return a ListView, so you don't need to implement it yourself.
  • onPause(): This method is first called when the user leaves the Fragment, and some changes need to be committed, because the user is likely not to return.
 
 

There are two ways
 to load Fragment into Activity :
  • Method 1: Add Fragment to Activity's layout file
  • Method 2: Dynamically add Fragment in the code of Activity (recommended)

The first method is simple but not flexible enough. Adding Fragment to the layout file of Activity is equivalent to binding Fragment and its view with the view of the activity, and the fragment view cannot be switched during the life cycle of the activity.
The second way is more complicated, but it is also the only way to control fragments at runtime (load, remove, replace).

转载请注明出处http://blog.csdn.net/htq__ https://blog.csdn.net/htq__/article/details/50949240

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326303282&siteId=291194637