Android-activity life cycle

Source: Internet
Author: User

I. Description

The main three states of activity:

Running (running): On the screen foreground (at the top of the current task stack)

Paused (paused): Loses focus but remains visible to the user (overwriting activity may be transparent or not completely obscured)

Stopped (STOP): completely covered by another activity

Ii. Chain of event methods

2.1 Entering activity

OnCreate, OnStart, Onresume

2.2 Back Key

OnPause, OnStop, OnDestroy

2.3 Home Key exit: OnPause, OnStop home key back: Onrestart, OnStart, Onresume

2.4 Hibernation/recovery

Hibernation: OnPause

Recovery: Onresume

2.5 Rotating the screen

Android:configchanges Not set:

OnStart, OnCreate, Ondestory, OnStop, OnPause, Onresume

Android:configchanges= "Orientation|keyboardhidden" is set:

The life cycle method is not triggered, see article here.

2.6来 Electric

Caller ID, show caller interface:

OnPause-OnStop

Close the phone interface and return to the current activity:

Onrestart, OnStart, Onresume

2.7 Other activity

Go to the next activity:

OnPause-OnStop

Return from other activity to current acitivity:

Onrestart, OnStart, Onresume

III. application scenarios combined with the activity life cycle

3.1 combined with broadcast (broadcast)

In the Onresume Register broadcast (Registerlinstener), the OnPause unregisters the broadcast (Unregisterlinstener). For example:

Do "shake" function (sensor), monitoring network changes, you can register in the Onresume monitoring, write-off in the OnPause, has saved resources to improve efficiency.

3.2 In combination with services (service)

In the OnStart binding service (Bindservice), unbind (Unbindservice) in OnStop. For example:

The data on the UI needs to be updated through the service timing, and the activity's visible period is between OnStart and OnStop, so you can start the service again OnStart and stop the service at OnStop. In addition to improving the user experience, developers should optimize the program as much as possible in order to conserve system resources.

3.3 Combined with cursor

Use Managedquery to let activity help you manage the cursor life cycle without having to close it yourself. But there are some questions that add two articles: here 1, here 2.

3.4 Releasing Resources

You can release some resources in the ondestory. For example, you can call MediaPlayer's release at Ondestory.

Iv. attention

4.1 The implementation of all activity life cycle methods must first call its parent class version.

4.2 The Onresume and OnPause methods should be lightweight because the activity often toggles between pausing and resuming.

4.3 In the system in some emergency situations need to reclaim memory, OnStop, ondestory may not be called, so you need to save long-term data in the OnPause.

Android-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.