Android Development Series Two window activity lifecycle _android

Source: Internet
Author: User

In the previous article to introduce you to the Android Development series of a button to achieve display time, interested friends can click to read the details.

The 7 lifecycle methods that need to invoke 7 life cycles at different stages from creation to destruction are defined as follows:

protected void OnCreate (Bundle savedinstancestate)
protected void OnStart ()
protected void Onresume ()
protected void OnPause ()
protected void OnStop ()
protected void Onrestart () protected
void OnDestroy ()

The above 7 life cycle methods are called in a certain order in 4 stages, respectively.

1, start the activity: At this stage, execute the 3 lifecycle methods, namely Oncreate,onstart,onresume

2,activity loses focus: If the activity gets focused, the current activity loses focus if it enters another activity or application, and this phase executes the Onpause,onstop method

3,activity regain Focus: The Onrestart,onstart,onresume method is executed sequentially

4, close the activity: when the activity is closed, the system executes the 3 lifecycle methods sequentially, respectively, onpause,onstop,ondestory

If no state changes occur during the 4 phases of the lifecycle method execution, the system executes the 4-phase lifecycle method in the order listed above, and if the state is changed during execution, the system invokes the lifecycle method in a more complex way

If the activity regain focus and then lose focus during the execution of the OnStop method, the system will not perform the Ondestory method, but rather the life cycle method in the following order

Onstop->onrestart->onstart->onresume->onpause->onstop

The following figure describes the process by which an activity invokes a lifecycle method after a change from creation to destruction and intermediate state


A schematic diagram of the activity lifecycle call method shown above shows that there are two loops in the life cycle of the entire activity, the first cycle is onpause->onresume->onpause, the second layer is onstop-> Onrestart->onstart->onresume->onpause->onstop. We can consider these two layers of loops as child lifecycles throughout the activity lifecycle.

The first loop is called the focus life cycle, and the second cycle is called the visual life cycle, that is, the first layer of cyclic activity focuses on the acquisition and loss of the process, in which the activity is always visible, The second cycle is a cycle in which the activity is visible and invisible, along with the acquisition and loss of the activity focus, in other words, the activity is first shown, then the focus is lost, Finally, the current activity becomes invisible because of the other activity that pops up.

Therefore, the activity has the following 3 life cycles:

1. Overall life cycle: Oncreate->....->ondestroy

2. Visual life cycle: Onstart->....->onstop

3. Focus Life cycle: Onresume->....->onpause

The following code outputs log information in 7 lifecycle methods in the Activity class, respectively

Package com.neil.ad02;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate
(savedinstancestate);
LOG.D ("OnCreate", "OnCreate method is executed");
Setcontentview (R.layout.activity_main);
} @Override protected void OnDestroy () {Super.ondestroy ();
LOG.D ("OnDestroy", "OnDestroy method is executed");
} @Override protected void OnPause () {super.onpause ();
LOG.D ("OnPause", "OnPause method is executed");
} @Override protected void Onrestart () {Super.onrestart ();
LOG.D ("Onrestart", "Onrestart method is executed");
} @Override protected void Onresume () {super.onresume ();
LOG.D ("Onresume", "Onresume method is executed");
} @Override protected void OnStart () {Super.onstart ();
LOG.D ("OnStart", "OnStart method is executed");
} @Override protected void OnStop () {super.onstop ();
LOG.D ("OnStop", "OnStop method is executed"); }
}

Open Android Device Monitor watch

The red box in the picture is to regain focus, and the rest of you can try

The above content is small to introduce the Android Learning series two of the life cycle of the activity, I hope that the above help!

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.