The life cycle of components in Android

Source: Internet
Author: User

1. Activity life cycle Diagram

Ii. activity three states

(1) Active: When activity is running on the screen foreground (at the top of the current task activity stack), it gets the focus to respond to the user's actions , is active, and only one activity is active at the same time ( Active).

(2) Paused: It is paused when the activity loses focus but remains visible to the user (such as when there is another transparent activity or toast, alertdialog, etc.) on top of it. The paused activity is still alive (it retains all state and member information and remains connected to the window manager), but can be killed by the system when the system memory is very small.

(3) stoped: stopped when completely obscured by another activity , it still retains all state and member information in memory. It is not visible to the user, it is often killed by the system when memory is needed elsewhere.

3. Activity Seven Methods

OnCreate (): When the activity is instantiated for the first time, the system is called, and the entire life cycle is called only 1 times. Typically used to initialize settings , set the layout file to be used for activity, and static settings such as a button binding listener.

OnStart (): The system is called when activity is not visible and the user focus cannot be interacted with.

Onrestart (): The system is called when activity has stopped and then restarted.

Onresume (): The system is called when activity is visible and the user's focus can interact .

OnPause (): Used to store persistent data . To this step is visible but not interactive, the system will stop the animation and other CPU-consuming things. As you know from the above description, you should save some of your data here, because your program's priority is reduced and may be withdrawn by the system.

OnStop (): Called by the system when the activity is completely overwritten by the new activity.

OnDestroy (): When the activity (the user calls finish () or the system is destroyed by the system due to low memory) the system is called, (the entire life cycle is called only 1 times) to release the resources created in the OnCreate () method, such as the end thread.

4. Android three nested loops

(1) Activity complete life cycle: Starting from the first call to OnCreate () until the call OnDestroy () ends.

(2) The visual life cycle of the activity: from calling OnStart () to the corresponding call to OnStop (). Between the two methods, you can maintain the resources needed to display the activity. If you register a broadcast receiver in OnStart () to listen for changes that affect your UI, log off in OnStop ().

(3) The activity's foreground life cycle: from calling Onresume () to the corresponding call to OnPause ().

5. Broadcastreceiver Broadcast receiver life cycle

The life cycle is only about 10 seconds, if within onreceive () do more than 10 seconds of things, you will report the ANR (Application no Response) program unresponsive error message . Its life cycle ends when the callback OnReceive () method starts and the method returns results.

6. Service life cycle

The service's full life cycle begins with calling OnCreate () until the end of the call to OnDestroy ().

Service is available in two ways: (Details visible http://www.cnblogs.com/mengdd/archive/2013/03/24/2979944.html)

(1) Start with call Context.startservice () and End with call Context.stopservice (). In this case, the service is created by calling StartService () from another component. This service can run indefinitely, and you must call the stopself () method or other component to call the StopService () method to stop it. When the service is stopped, the system destroys it.

(2) Set to call the Context.bindservice () method to call Context.unbindservice () to close. In this case, the service is created by calling Bindservice () from another component (a customer). Customers can communicate via a IBinder interface and service. Customers can use the unbindservice () method to close this connection. A service can be bound to multiple customers at the same time, and the service is destroyed when multiple customers are unbound.

7, an activity of the start-up process

(1) Start order of first activity: OnCreate ()-->onstart ()-->onresume ()

(2) When another activity starts: The first activity OnPause () and the second activity OnCreate ()-->onstart ()-->onresume ()-- First Activity OnStop ()

(3) When returning to the first activity: the second Activity onPause ()--The first activity Onrestart ()-->onstart ()-->onresume ()-- Second Activity onStop ()-->ondestroy ()

Each activity is in a certain state, and for the developer it is impossible to control the application in a certain state, which is done by the system.

The life cycle of components in Android

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.