Android Development-Activity life cycle

Source: Internet
Author: User

This chapter discusses the life cycle of activity



Activity has four states throughout its life cycle, 7 methods, 3 nested Loops


State:

1. Activity/running Activity Status

Refers to the activity state that the current user is interacting with

2. Paused Suspend status

Refers to the current activity visible, but the state is masked by a toast or dialog box

3. Stopped Stop State

Refers to the state of being obscured by the newly created activity, which retains the previous operation information and state, but may be killed if the system memory is not enough

4. Dead Non-active status

When the activity has not been started or is inactive when the system is reclaimed, you can call the finish () method manually to end the activity


Life cycle:

1. OnCreate ();

is called when the activity is first created, you can do some variable initialization, bind the data, show the view

2. OnStart ();

After the activity is created, start displaying the view to the interface

3. Onrestart ();

This method is called when the view is re-displayed when the activity is stopped or interrupted

4. Onresume ();

After the view is displayed in the interface, you can interact with the user

5. OnPause ();

When jumping to another activity, or before stopping, or before destroying the call

6. OnStop ();

Called when it is obscured by another activity,

7. OnDestroy ();

Called when activity is destroyed


Nested loops:

1. Activity Full life cycle

From OnCreate () to OnDestroy ()

2. Activity Visual life cycle

From OnStart () to OnStop ()

3. Activity foreground life cycle

From Onresume () to OnPause ();


For example, using the Logcat print life cycle process is

1. Full Life cycle

OnCreate (), OnStart (), Onresume (), OnPause (), OnStop () Y ()

2. Running activity, the life cycle process is

OnCreate (), OnStart (), Onresume ()

3. When the life cycle of the screen is switched

Onsaveinstancestate (), OnPause (), OnStop (), OnDestroy (), OnCreate (), OnStart () Nrestoreinstancestate (), Onresume

4. When we add android:configchanges= "orientation" to the corresponding activity in Androidmenifest.xml

The life cycle will only execute the onconfigurationchanged () method,

5. One activity jumps to another activity declaration cycle process is

First activity

Onsaveinstancestate ()

OnPause ()

Start a second activity

OnCreate ()

OnStart ()

Onresume ()

The first activity stopped.

OnStop ()

At this point, the first activity is returned

Onrestart ()

OnStart ()

Onresume ()

At this point, the second activity destroys the

OnStop ()

OnDestroy ()


Summary: When the activity jumps to the new activity, the old activity is saved to the scene and stopped, the new activity is created, and if the old activity is returned, the new activity is destroyed. The old activity executes the Onrestart () method, then displays the interface and interacts with the state, so the OnCreate () method is only called once, but when switching between the two screens, it is equivalent to re-executing the life cycle, destroying the object before the screen. Recreate the new activity, if the android:name= "orientation" is configured in the manifest, then the Onconfigurationchanged () method is executed, and the life cycle is not re-executed again





Android Development-Activity life cycle

Related Article

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.