Android Development Learning Note (ii)-activity life cycle

Source: Internet
Author: User

One: Life cycle of activity:

Two: created->resumed

When an application's launch icon is clicked by the user, the application starts executing from the activity's OnCreate () method. (when there are multiple activity files, the main activity file specified in the manifest file is executed).

When the OnCreate () method is executed, a quick call to the OnStart (), Onresume () method allows the user to perform related operations.

Take as little action as possible in the OnCreate () method to prevent the program from running for a long time without entering the interface.

Three: resumed->paused && paused->resumed

When another activity is started causing partial blocking (visible in the main activity section), the main activity calls the OnPause () method into the paused state. When you need to go back to the main activity, you can go back to the screen by calling the Onresume () method and perform the relevant action.

After an activity enters the paused state, it is possible for him to return to the screen, possibly into the stopped state, and then go into the destryed state to close.

When activity enters the paused state, it should stop the operation of the pause state activity in the OnPause () method, releasing the appropriate resources, and the resources the user expects to save.

When activity returns to the resumed state, the resources released in the paused state should be initialized

Four: paused->stopped && stopped->started

When activity is partially blocked into a full block (activity is not visible), the main activity calls OnStop () into the stopped state. When you go back to the main activity, the Onrestart () method is called. Then call the Onresume () method quickly.

When activity enters the stopped state, the activity object is saved in memory and the saved activity is called when the activty reverts to the resumed state. Therefore, you do not need to initialize the corresponding components.

In the OnStop () method, you want to free the unwanted resources, and in the Onrestart () method, you re-create the freed resources.

V: Destroyed:
1 activity may be a normal exit (such as clicking the Back button/using the Finish () method to exit), it is also possible to exit abnormally (stop state is not used for a long time/the foreground activity requires more resources).

When you exit normally, the relevant data is not saved, but when you exit abnormally, the relevant data is saved and the data is recovered the next time the activity instance is created.

The data stored by the system to recover the relevant data is called instance state. The instance state is stored in the Key-value pairs in the bundle object.

When the activity exits abnormally, the relevant data is saved to the object bundle via Onsaveinstancestate (). When this activity is re-created, the bundle object is passed to OnCreate () and Onrestoreinstancestate (). The data is recovered through the bundle object.

Reference: Http://hukai.me/android-training-course-in-chinese/basics/activity-lifecycle/index.html.

Android Development Learning Note (ii)-activity 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.