Android Fragment learning note ② (Fragment life cycle)

Source: Internet
Author: User

I. Fragment life cycle Diagram

two. Introduction to the Fragment life cycle approach

The life cycle of the fragment is similar to the activity life cycle, and its lifecycle approach is as follows.

Onattach: Bind to activity (called when fragment and activity are associated).
OnCreate: Create fragment
Oncreateview: Creates a fragment layout (called when the layout is loaded for fragment).
Onactivitycreated: After the activity is created (called when the OnCreate method in the activity finishes executing).
OnStart: Visible, non-interactive
Onresume: Visible, can be interactively
OnPause: Partially visible, non-interactive
OnStop: Not visible
Ondestroyview: Destroys the Fragment View object (called when the layout in fragment is removed).
OnDestroy: Fragment destroyed the
Ondetach: is unbound from activity (called when fragment and activity disassociate).

The following content is from http://www.cnblogs.com/purediy/p/3276545.html

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.

Android Fragment learning note ② (Fragment life cycle)

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.