[Whole] Android Fragment life cycle diagram

Source: Internet
Author: User

1. Onattach------Called once the fragment is associated with its activity

2. onCreate-------called to do initial creation of the fragment

3. Oncreateview---Creates and returns the view hierarchy associated with the fragment

4.onActivityCreated---tells the fragment, its activity have completed its own {@link activity#oncreate activi Ty.oncreaate}

5.onStart----------makes the fragment visible to the user (based in its containing activity being started)

6.onResume-----makes the fragment interacting with the user (based on its containing activity being resumed).

7. OnPause-----Fragment is no longer interacting with the user either because it activity is being paused or a fragment Operation is modifying it in the activity.

8.onStop------Fragment is no longer visible to the user either because its activity was being stopped or a fragment operation is modifying it In the activity.

9.onDestroyView------Allows the fragment to clean up resources associated with its View.

10.onDestroy---------called to do final cleanup of the fragment ' s state.

11.onDetach--------called immediately prior to the fragment no longer being associated with its activity

Fragment and activity life cycle comparison chart:

1. When a fragment is created, it goes through the following States.

    • Onattach ()
    • OnCreate ()
    • Oncreateview ()
    • Onactivitycreated ()

2. When the fragment is visible to the user, it will experience the following States.

    • OnStart ()
    • Onresume ()

3. When this fragment enters "background mode", it will experience the following status.

    • OnPause ()
    • OnStop ()

4. When the fragment is destroyed (or the activity that holds it is destroyed), it goes through the following States.

    • OnPause ()
    • OnStop ()
    • Ondestroyview ()
    • OnDestroy ()//Originally omitted class this callback, thanks to xiangxue336 proposed.
    • Ondetach ()

5. Just like Activitie, in the following States, you can use the bundle object to save an fragment object.

    • OnCreate ()
    • Oncreateview ()
    • Onactivitycreated ()

6. Most of the state of fragments is similar to Activitie, but fragment has some new states.

    • Onattached ()--called when fragment is added to the activity (the activity is available in this method).
    • Oncreateview ()-When the activity is going to get fragment layout, call this method, where fragment creates its own layout (interface).
    • Onactivitycreated ()--Call this method when the activity's oncreated () method returns
    • Ondestroyview ()-this method is called when the view in the fragment is removed.
    • Ondetach ()--this method is called when fragment and activity are separated.

Once the activity enters the resumed state (that is, the running state), you are free to add and remove fragment. Therefore, the life cycle of fragment can be operated independently only when the activity is in the resumed state, and other times depend on the activity's life cycle change.

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.