Android Knowledge Points: life cycle of activity

Source: Internet
Author: User

The state that the application (or activity) executes is called a process (progress). In the Android operating system, each application is a process. The Android framework maintains a unique activity history stack and observes each application process. At the same time, the system platform will manage the system memory according to the system's memory usage and activity state. In addition to running the program flow and operating system interface components, the activity category is most important because it provides the developer with the function to control the process life cycle.

The life cycle of an Android application is managed by the Android framework, rather than by the application's own direct control. Usually each application (the portal is usually an activity OnCreate method), will occupy a process. When the system memory is about to be insufficient, the process is automatically recycled in priority order .

In addition to the OnCreate method, an activity class pre-defines basic behaviors such as onpause (the behavior when the process is paused), Onresume (the behavior of the continuation process), and so on. When you switch from one activity to another activity, the original activity passes through a series of state changes. We can add a number of processes in the program that correspond to each state, and each time the activity changes its state, it executes the corresponding process.

Status of activity

Android virtual machines are managed using stacks (stack based). There are 4 main types of states:

    • Active (Active)
    • Paused (pause)
    • Stopped (STOP)
    • Dead (reclaimed or not started)
Active (Active)

The active state is the state in the activity running after the user starts the application or activity.

On the Android platform, only one activity is active or running (Running) at the same time. Other activity is in the Dead, Stop (Stopped), or pause (Paused) state.

Paused (pause)

Paused status is currently running screen image temporarily dark down, back to the state of the background screen.

When we use "Toast", "Alertdialog" or a phone call, we will return the activity that we have run back to the background screen. The new "Toast", "Alertdialog" and other interface components cover the original activity interface. When the activity is in the paused state, the user cannot interact with the original activity.

Stopped (STOP)

The stopped state is that other activity is executing, and the activity has left the screen and no longer has a status in progress.

By long pressing the "Home" key, you can bring up all the applications in the stopped state. Activity in the stopped state can also be awakened by "Notification".

Dead (reclaimed or not started)

The dead state is a state in which activity has not been started or has been manually stopped, or has been automatically reclaimed by the system.

To terminate the activity manually, you can call the finish () function in the program. If it is automatically reclaimed by the system, it indicates that the system is running out of memory, so the system will reclaim the memory occupied by the activity in the stopped state according to the low-memory recycling rules.

Behavior when system memory is low

When the Android system is low on memory, the memory is recycled according to the following rules:

    • Reclaim processes unrelated to other activity or service/intent receiver, i.e. prioritize the recovery of independent activity
    • Recycle other types of activity that are in the stopped state (activity waiting in the background). The longest unused activity will be recycled (the official claim is based on the "LRU algorithm")
    • It's not enough? Recycle service Process
    • Not yet? Turn off the visible activity/process
    • Close the current Activity

When the system memory is missing to close the "visible activity/process", then it is necessary to consider changing the phone.

Observe the activity running process
public class BMI extends Activity {    private static final String TAG = "BMI";    public void OnCreate () {super.oncreate (...); LOG.V (TAG, "onCreate");}    public void OnStart () {        super.onstart ();        LOG.V (TAG, "OnStart");    }    public void Onresume () {        super.onresume ();        LOG.V (TAG, "Onresume");    }    public void OnPause () {        super.onpause ();        LOG.V (TAG, "OnPause");    }    public void OnStop () {        super.onstop ();        LOG.V (TAG, "onStop");    }    public void Onrestart () {        super.onrestart ();        LOG.V (TAG, "Onrestart");    }    public void OnDestroy () {        Super.ondestroy ();        LOG.V (TAG, "OnDestroy");}    }

The above seven states can be summed up into three groups:

1. Resource Allocation (Create/destroy)

The full activity life cycle starts with the "Create" state and ends with the "Destroy" state. Allocate resources when creating (create), and release resources when destroying (Destroy).

2. Visible and Invisible (start/restart/stop)

When the activity runs to the "Start" state, you can see the activity on the screen. Conversely, when activity runs to the "Stop" state, the activity disappears from the screen.

When the user presses the Back button to return to the previous activity, it first goes to the restart State and then to the normal start state.

3. User can access the screen directly (Resume/pause)

When there is a toast, alertdialog, SMS, telephone and other messages in the chaos, the original activity will enter the "Pause" state, temporarily abandoned the ability to directly access the screen, was interrupted to the background, the foreground to high priority events. When these high-priority events are processed, the activity goes into a "Resume" state, where the screen is accessed directly.

The actual function name is prefixed with the method named on start, and the activity's life cycle state diagram is as follows:

Activity Operation Process

Judging by the actual running records, we can summarize the action flow that all Android applications follow:

General Startup
    • onCreate, OnStart, Onresume

The basic process for starting an activity is to assign a resource to the activity (create state) and then display the activity content on the screen (start state). After everything is ready, take control of the screen (resume status) and users can start using the program.

Call another activity
    • OnPause (1), OnCreate (2), OnStart (2)-Onresume (2), OnStop (1)

This is the process of freezing the original activity before surrendering the ability to directly access the screen (pause state). The Activity1 will not be stopped until Activity2 completes the general startup process.

Back to original activity
    • OnPause (2), Onrestart (1), OnStart (1), Onresume (1), OnStop (2), OnDestroy (2)

In the new Activity2, click the Back button of the hardware, and return to the original Activity1. Click the Back button to return to the original activity.

Exit end
    • OnPause, OnStop, OnDestroy

If the program has a direct call to "finish" function to close the activity, the system assumes we are very sure what we are doing, so we will skip the first freeze (Freeze) stage, pause (pause), stop (stop), and then Destroy (Destroy).

Recycle and then start
    • onCreate->onstart->onresume

Once the recovered activity is re-called, the activity's OnCreate function is called again as if it were a normal boot. This is the same process as the general start in the first step.

When we use the "Android" phone for a while, there have been multiple applications on the phone. With the "Back" button pressed, "Android" will open the activity that was last opened.

What happens when we press the "back" button multiple times, and in theory sooner or later return to an activity that has been destroyed (Destroy)?

If the activity that should be opened has been recycled, then the activity will be created again (create) and the activity created again will not be the same as the activity we opened.

So if you want the activity to be created again to look the same as it was originally opened, then when switching between activity, we need to keep an eye on the data: it's best to save the data each time the activity runs to the "OnPause" or "onStop" state, and then "OnCreate" When the data is read out.

Android Knowledge Points: life cycle of activity

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.