On the activity running process of Android (life cycle)

Source: Internet
Author: User

Regarding the activity running process of Android, we can write some programs to visually view the activity running process. Here we use the Log tool to get the activity run log. If we create a new Android project, the Processactivity.java code is as follows:

public class Processactivity extends Activity {

private static final String tag= "Xu Shouwei";

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

LOG.V (Tag, "onCreate ...");

}

@Override

protected void Onrestart () {

TODO auto-generated Method Stub

Super.onrestart ();

LOG.V (Tag, "Onrestart ...");

}

@Override

protected void OnStart () {

TODO auto-generated Method Stub

Super.onstart ();

LOG.V (Tag, "OnStart ...");

}

@Override

protected void Onresume () {

TODO auto-generated Method Stub

Super.onresume ();

LOG.V (Tag, "Onresume ...");

}

@Override

protected void OnPause () {

TODO auto-generated Method Stub

Super.onpause ();

LOG.V (Tag, "OnPause ...");

}

@Override

protected void OnStop () {

TODO auto-generated Method Stub

Super.onstop ();

LOG.V (Tag, "onStop ...");

}

@Override

protected void OnDestroy () {

TODO auto-generated Method Stub

Super.ondestroy ();

LOG.V (Tag, "OnDestroy ...");

}

}

We added a "log" record message for the activity's brother status. When the emulator is running, we can use the "LogCat" tool to view the status of the activity. The seven states in the above code can be summed up into three categories:

1. Resource allocation (create/destory)

The full activity life cycle starts with the "Create" state and ends with the "destory" state. Allocates resources when creating (create), and frees 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 activity has not been destroyed (destory) and is called again, it will enter the "Restart" state before entering the normal "Start" state. For example, when we return to the previous activity from the current activity, we enter a "Rstart" state more than the direct opening of the new activity.

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

When there is a "Toast", "Alertdialog", SMS, telephone and other messages into the time, 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 hours are processed, the activity goes to the "Resume" state, and the screen is accessed directly.

We can deepen our understanding through one of the above processes:

By the time the record is run, we can summarize the action flow that all Android apps follow.

(1) General start-up

Flow: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 the user can start using the program.

(2) Call another activity

Flow:onpause (1), OnCreate (2), OnStart (2), Onresume (2), OnStop (1)

This is a county to freeze the original activity, and then surrender the ability to directly access the screen (pause state) right. Knowing that Activity2 completes half of the start-up process, Activity1 will not be stopped.

(3) Return to the original activity

Flow:onpause (2), Onrestart (1), OnStart (1), Onresume (1), OnStop (2), Ondestory (2)

In the new activity, clicking the "Back" button on the hardware allows us to return to the original activity.

(4) End of exit

Flow:onpause, OnStop, ondestory

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

(5) After recovery and restart

Flow:oncreate, OnStart, Onresume

Once the recovered activity is called again, the activity's OnCreate function is called again as if it were a normal startup.

When we use an Android phone for a while, multiple applications have been executed on the phone. Just press the Back button, and Android will open the activity that was last opened.

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

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

So if you want the activity to be created again to look the same as the one that was originally opened, when switching between activity, we should be mindful of preserving the data: It is best to save the data each time the activity runs to the "Onpalse" or "onStop" state, and then "OnCreate" time to read the data out!

Talking about the activity running process of Android (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.