The life cycle of fragment

Source: Internet
Author: User

The life cycle of the fragment is 11, more complex than the life cycle of the activity, and the fragment life cycle is carried out in conjunction with the life cycle of the activity, see figure:



The background is the life cycle of the activity, the other is the fragment cycle, as can be seen in the diagram, the same life cycle is performed before the fragment initialization is completed, and when the cycle of destruction is entered, it becomes the first execution of the fragment.


1. public void Onattach (activity activity) method

After executing the method, fragment is associated with the activity, where it is possible to get the associated activity object through Getactivity (), but the control is not loaded yet, so no controls in the associated activity can be obtained.

2. public void OnCreate (Bundle savedinstancestate)

This method prepares the view in the load fragment, the Savedinstancestate parameter is the parameter of the recovery state, similar to the parameter in the activity's oncreate, and cannot get to the activity's control until this method is executed.

3. Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate)

This method is the most important one in the fragment life cycle, where the view that fragment is to be displayed is loaded, the individual controls in the view can be obtained in the method, and the control's display is controlled, and the parameter container is the parent view of the view to be loaded. However, after the execution of the method has ended, you still cannot get the other controls in the activity except for the current fragment.

4. public void onactivitycreated (Bundle savedinstancestate)

After executing this method, the window has already been initialized, and it is only possible to get all the controls in the activity by using the Getactivity (). Findviewbyid () method.

5. public void OnStart ()

6. public void Onresume ()

7. public void OnPause ()

8. public void OnStop ()

This four method is similar to the one corresponding to the activity life cycle.

9. public void Ondestroyview ()

After calling this method, the view created in Oncreateview will be detached from the fragment.

10. public void OnDestroy ()

The fragment cannot be manipulated at this time, but fragment is still associated with the activity.

11. public void Ondetach ()

The last method of this fragment life cycle, after executing this method, fragment is not associated with the activity, and the associated view will be destroyed.


The life cycle of fragment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.