The difference and analysis of Android activity life cycle and fragment life cycle

Source: Internet
Author: User

Android Fragment life cycle diagram:

Activity Life Cycle Graph:

Contrast chart:

Fragment Life cycle Analysis:

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.

Activity Life Cycle Analysis:

The above 7 life cycle methods are called in a certain order in 4 stages, and these 4 phases are as follows:

    1. Start activity: Perform 3 life cycle methods in sequence at this stage: OnCreate, OnStart, and Onresume.
    2. Activity loses focus: If you enter another activity or application while the activity is focused, the current activity loses focus. At this stage, the OnPause and OnStop methods are executed in turn.
    3. Activity regain focus: If activity regain focus, 3 life cycle methods are executed sequentially: Onrestart, OnStart, and Onresume.
    4. Close activity: When activity is closed, the system executes 3 life cycle methods: OnPause, OnStop, and OnDestroy.

If there is no state change during these 4 phases of the life cycle approach, then the system will execute the life cycle methods in the 4 stages as described above, but if the state is changed during execution, the system will invoke the life cycle method in a more complex way.

The life cycle methods that can change the execution trajectory of the system during execution are OnPause and OnStop. If the activity gets the focus again during the execution of the OnPause method, it loses focus. Instead of executing the OnStop method, the system executes the corresponding life cycle method in the following order:

OnPause, onresume-> OnPause

If the activity gets the focus again during the execution of the OnStop method, it loses focus. Instead of executing the OnDestroy method, the system executes the corresponding life cycle method in the following order:

OnPause, Onresume, OnStart, Onrestart, OnStop, OnStop

As you can see in the activity life cycle shown in Figure 2, the system invokes the OnPause, OnStop, and Ondesktroy methods when terminating the application process. The OnPause method is at the top of the line, meaning that the activity may be terminated when the focus is lost, while the OnStop and OnDestroy methods may not have a chance to execute. Therefore, the current activity state should be saved in the OnPause method to ensure that the code that holds the activity state can be executed at any time when the process is terminated.

The difference and analysis of Android activity life cycle and 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.