Two minutes to give you a thorough understanding of the Android activity Lifecycle (illustrated) _android

Source: Internet
Author: User
Hello everyone, today to explain the life cycle of Android activity, I have also talked about this aspect of the content, but like most articles on the Web, the basic translation of the Android API, too general, I believe that everyone looked, there will be a little help, But I can't fully grasp it, so I'm going to summarize it today.
First take a look at the activity lifecycle diagram provided in the Android API (not clear, you can see the full article, look back at this diagram, you will understand):



The activity actually inherits the ApplicationContext class, and we can rewrite the following methods, as follows:

Copy Code code as follows:

public class activity extends ApplicationContext {
protected void OnCreate (Bundle savedinstancestate);

protected void OnStart ();

protected void Onrestart ();

protected void Onresume ();

protected void OnPause ();

protected void onStop ();

protected void OnDestroy ();
}

In order to facilitate better understanding, I simply wrote a demo, do not understand the activity cycle of friends, can hands-on practice, everyone follow my steps.
Step one: Create a new Android project, which I'm naming Activitydemo.
Step Two: Modify Activitydemo.java (I have written the above seven methods, mainly in log printing), the code is as follows:
Copy Code code as follows:

Package Com.tutor.activitydemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
public class Activitydemo extends activity {

private static final String TAG = "Activitydemo";

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

LOG.E (TAG, "Start oncreate~~~");
}

@Override
protected void OnStart () {
Super.onstart ();
LOG.E (TAG, "Start onstart~~~");
}

@Override
protected void Onrestart () {
Super.onrestart ();
LOG.E (TAG, "Start onrestart~~~");
}

@Override
protected void Onresume () {
Super.onresume ();
LOG.E (TAG, "Start onresume~~~");
}

@Override
protected void OnPause () {
Super.onpause ();
LOG.E (TAG, "Start onpause~~~");
}

@Override
protected void OnStop () {
Super.onstop ();
LOG.E (TAG, "Start onstop~~~");
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
LOG.E (TAG, "Start ondestroy~~~");
}

}

Step three: Run the above project, the effect diagram is as follows (nothing special):



The core is in the Logcat window, if you still don't use logcat you can look at my article Android Development notes: Log text (LOG.V,LOG.D,LOG.I,LOG.W,LOG.E), When we opened the application, we implemented the OnCreate ()->onstart ()->onresume three methods and looked at the Logcat window as follows:

Back key:
When we press the Back button, our application will end, and then we will call OnPause ()->onstop ()->ondestory () Three methods, as shown in the following figure:

Home key:
When we open the application, such as the browser, I was browsing the NBA news, when I saw half, I suddenly want to listen to songs, then we will choose to press the Home button, and then to open the music application, and when we press home, the activity has executed OnPause ()-> OnStop () The two methods, when the application is not destroyed. As shown in the following illustration:


When we started the Activitydemo application again, we executed the Onrestart ()->onstart ()->onresume () Three methods separately, as shown in the following illustration:

Here we will draw a question that when we press the home key and then enter the Activitydemo application, the status of our application should be the same as the state before the home key, and also for the convenience of understanding, here I will make some changes to the Activitydemo code, is to add a edittext.
Fourth step: Modify the Main.xml layout file (add a edittext), the code is as follows:

Copy Code code as follows:

The core in the Logcat window, if you still do not use logcat you can look at my article Log text detailed (LOG.V,LOG.D,LOG.I,LOG.W,LOG.E), we open the application has executed successively OnCreate ()-> OnStart ()->onresume three methods, take a look at the Logcat window as follows:
Back key:
When we press the Back button, our application will end, and then we will call OnPause ()->onstop ()->ondestory () Three methods, as shown in the following figure:
Home key:
When we open the application, such as the browser, I was browsing the NBA news, when I saw half, I suddenly want to listen to songs, then we will choose to press the Home button, and then to open the music application, and when we press home, the activity has executed OnPause ()-> OnStop () The two methods, when the application is not destroyed. As shown in the following illustration:
When we started the Activitydemo application again, we executed the Onrestart ()->onstart ()->onresume () Three methods separately, as shown in the following illustration:

Here we will draw a question that when we press the home key and then enter the Activitydemo application, the status of our application should be the same as the state before the home key, and also for the convenience of understanding, here I will make some changes to the Activitydemo code, is to add a edittext.

Fourth step: Modify the Main.xml layout file (add a edittext), the code is as follows:

Fifth step: Then the other unchanged, run the Activitydemo program, in the EditText input such as "Frankie" string (the following figure:)



At this point, you can click the Home button, and then start the Activitydemo application again, this time edittext did not enter the word "Frankie", the following figure:

This is clearly not a qualified application, so we need to implement it in several ways, as shown in step sixth:
The sixth step is to modify the Activitydemo.java code as follows:

Copy Code code as follows:

At this point, you can click the Home button, and then start the Activitydemo application again, this time edittext did not enter the word "Frankie", the following figure:
This is clearly not a qualified application, so we need to implement it in several ways, as shown in step sixth:
The sixth step is to modify the Activitydemo.java code as follows:

Seventh: Rerun the Activitydemo program, repeat the fifth step, when we press the home key, restart the application, EditText has the last entered the "Frankie" words, as shown in the following figure:

OK, the basic success, at this time we can look back at the Activity life Cycle chart, I think we should fully understand the activity life cycle, do not know you understand?

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.