Life cycle of activity

Source: Internet
Author: User

First look at a diagram of the classic activity life cycle


You can see the process of the activity's life cycle, with a few specific processes:

The Oncreate,onstart and Onresume methods are called successively at 1.Activity startup.

2. When activity has a transparent layer of activity, it invokes the current activity's OnPause method and the new activity's Oncreate,onstart and Onresume methods.

3. When an activity is covered with an opaque activity, the current activity's Onpause,onstop method and the new activity's Oncreate,onstart,onresume method are called successively.

4. When the lock screen is called, the current activity's Onpause,onstop method is invoked.

5. Press the back key to invoke the Onpause,onstop,ondestroy method of the current activity.

6. Call the Onpause,onstop method of the current activity when you press the home key

7. Pressing the home key before the process kills the activity does not call any life cycle methods (that is, do not call the Ondestory method).

8. Activity does not call any method when there is a alertdialog on the activity.


For the entire life cycle, OnCreate and OnDestroy are paired, one creates a destroy and can only be called once, OnStart and OnStop are paired, one is called when the user is visible, one is called when the user is not visible, and can be called multiple times; o Nresume and OnPause are paired, one is called when the current activity is in the foreground, and one is called when the current activity is not in the foreground;

Here is a demo of the Test Activity life cycle:

This demo has three classes of mainactivity,standardactivity,transparentactivity.

Mainactivity is the main interface with three buttons to jump to Standardactivity, transparentactivity, and open a dialog box.

Transparentactivity is a transparent activity.

Standardactivity is an opaque activity.

Three activity all rewrite all life cycle methods and onwindowfocuschanged

Test Steps and Results:

1. Start the program

2. Start transparentactivity from the main interface

3. Return to the main interface in Transparentactivity

4. Start standardactivity from the main interface

5. Return from standardactivity to main interface


6. Open the dialog box from the main interface


7. Close the dialog box

Ibid.

8. Press the back key in the main interface


9. Press the Home button in the main screen


10. Kill in Process (do not call any life cycle method)

11. Press the home key in Standardactivity


12. Press the home key in Transparentactivity


13. Re-enter the main screen after pressing the home button on the main screen


14. Re-enter transparentactivity after Transparentactivity press the home key


Summary: The Onresume and OnPause methods are called when the activity is the foreground process, and onstart and OnStop are invoked when the activity visual process becomes a background process; O Nrestart is called when activity changes from a background process to a visual process without being destroyed;

Life cycle of activity

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.