1. Fragment Lifecycle
Fragment cannot be displayed on the mobile phone separately. It can only be displayed in the Activity. When the Activity is loaded, the onCreate () method is executed for the first time.
In one sentence, the lifecycle of Fragment is summarized as follows:
Fragment first needs to create an object (that is, instantiate an object), then load the layout, and then start running. When an Activity is created, the Fragment element is also created. When the Activity is destroyed, the entire Fragment lifecycle ends.
The following figure lists the lifecycle methods of Activity and Fragment:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgYWxpZ249 "center">
2. Relationship between Activity lifecycle and Fragment lifecycle.
The end of the Activity lifecycle is completed only after all Fragment ends the lifecycle of the Activity. That is, after all Fragment is destroyed, the Acivity will be destroyed by itself.
3. What is the difference between onCreate () and onCreateView?
The main difference is that the UI interface cannot be updated in the onCreate () method, because the layout has not yet been created, so the UI object in Fragment cannot be obtained, if the UI object cannot be obtained, the interface cannot be updated.
4. Fragment lifecycle Overview
Most Fragment lifecycles are similar to Activity lifecycles. Fragment and Activity have three similar lifecycles:
(1) onResumed: indicates that it is visible during the Activity. (2) onPaused: indicates that other activities have become the foreground and the focus has been obtained. (3) stoped: Indicates invisible, that is, Fragment is invisible when Activity is invisible. It may occur that the Activity is in the stoped state, the Fragment is removed from the Activity, and the Fragment is added to the rollback stack. This Fragment is also in the paused state. However, the Fragment in the stopped state is still alive. The member information of all States is always maintained. However, if it remains invisible for a long time, it may be killed by the Activity.
In general, the lifecycle of Fragment is directly affected by the lifecycle of Activity. For example, if the onPause () method is called in the Activity, every Fragment in the Activity will call the onPause () method.
Note:(1) The onCreate () method is called only once. (2) Update the UI in the onCreateView () method. The update cannot be performed in the onCreate () method, because the UI object cannot be obtained when this method is executed. (3) When the Activity is in the onStart (), onResumed (), onPause (), onStoped () state, all Fragment and Activity states are the same at this time. (4) When a View is destroyed, Fragment first destroys the View, and then destroys the View by itself. Finally, disassociated from Activity (5) and onAttach indicates that the Activity has been associated. If you need to pass a value, it can be passed in the onAttach () method of Fragment. (6) onActivityCreated (): indicates that the onCreate () method of the Activity is called when it returns. (7) onDestroyView (): indicates that the Fragment View has been removed. (8), onDetach () is the separation of Fragment from Activity. When the Activity receives the onCreate () status, this Fragment will only call the onActivityCreated () method in the Activity.