Two minutes to fully understand the Android activity life cycle (graphics)! Reproduced

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/android_tutor/article/details/5772285

  

Hello everyone, today to give you a detailed description of the activity of Android life cycle, I have said in front of this aspect of the content, but like most articles on the internet, basically are translated Android API, too general, I believe you see, there will be a little help, But it's not completely thoroughly understand, so I'm going to do it again today.

First look at the activity life cycle diagram provided in the Android API (not clear, you can see the full article, looking back at this diagram, you will understand):

The activity is actually inherited applicationcontext This class, we can rewrite the following method, the following code:

     Public classActivity extends ApplicationContext {protected voidonCreate (Bundle savedinstancestate); protected voidOnStart (); protected voidOnrestart (); protected voidOnresume (); protected voidOnPause (); protected voidOnStop (); protected voidOnDestroy (); }  

In order to facilitate the better understanding of everyone, I simply wrote a demo, do not understand the activity cycle of friends, you can practice, everyone follow my steps.

First step: Create a new Android project, which I named Activitydemo.

Step Two: Modify Activitydemo.java (I have re-written the above seven methods, mainly with log printing), the code is as follows:

Package Com.tutor.activitydemo;      Import android.app.Activity;      Import Android.os.Bundle;      Import Android.util.Log;  Public classActivitydemo extends Activity {Private StaticFinal String TAG ="Activitydemo";  Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                            Setcontentview (R.layout.main); LOG.E (TAG,"Start oncreate~~~"); } @Overrideprotected voidOnStart () {Super.onstart (); LOG.E (TAG,"Start onstart~~~"); } @Overrideprotected voidOnrestart () {Super.onrestart (); LOG.E (TAG,"Start onrestart~~~"); } @Overrideprotected voidOnresume () {super.onresume (); LOG.E (TAG,"Start onresume~~~"); } @Overrideprotected voidOnPause () {super.onpause (); LOG.E (TAG,"Start onpause~~~"); } @Overrideprotected voidOnStop () {super.onstop (); LOG.E (TAG,"Start onstop~~~"); } @Overrideprotected voidOnDestroy () {Super.ondestroy (); LOG.E (TAG,"Start ondestroy~~~"); }                }  

Step three: Run the above project as follows (nothing special):

Core in Logcat window, if you do not use logcat you can take a look at my article Log text (LOG.V,LOG.D,LOG.I,LOG.W,LOG.E), we opened the application has been executed OnCreate () OnStart ()->onresume three methods, take a look at the Logcat window as follows:

Back key:

When we press the back key, our application will end, and this time we will call OnPause ()->onstop ()->ondestory () Three methods as shown:

Home key:

When we opened the app, like a browser, I was browsing the NBA news, and when I saw half of it, I suddenly wanted to listen to the song, and then we chose to press the home button to open the Music app, and when we pressed home, the activity executed OnPause (). OnStop () These two methods, the application is not destroyed at this time. As shown in the following:

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

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

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

<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:text="@string/hello"/> <EditText Android:id="@+id/edittext"Android:layout_width="fill_parent"Android:layout_height="wrap_content"/> </LinearLayout>

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

At this time, you can press the Home button, and then start the Activitydemo application again, this time there is no edittext we entered the word "Frankie", such as:

This obviously cannot be called a qualified application, so we need to implement it ourselves in several methods of activity, as shown in the sixth step below:

The sixth step is to modify the Activitydemo.java code as follows:

Package Com.tutor.activitydemo;      Import android.app.Activity;      Import Android.os.Bundle;      Import Android.util.Log;      Import Android.widget.EditText;  Public classActivitydemo extends Activity {Private StaticFinal String TAG ="Activitydemo"; PrivateEditText Medittext; //defines a string type used to access the value of our edittext input        PrivateString mstring;  Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);              Setcontentview (R.layout.main); Medittext=(EditText) Findviewbyid (R.id.edittext); LOG.E (TAG,"Start oncreate~~~"); } @Overrideprotected voidOnStart () {Super.onstart (); LOG.E (TAG,"Start onstart~~~"); }          //When you press the home key and then start the app again, we want to restore the previous state@Overrideprotected voidOnrestart () {Super.onrestart ();              Medittext.settext (mstring); LOG.E (TAG,"Start onrestart~~~"); } @Overrideprotected voidOnresume () {super.onresume (); LOG.E (TAG,"Start onresume~~~"); }                    //when we press the home key, I assign the input value to mstring in the OnPause method@Overrideprotected voidOnPause () {super.onpause (); Mstring=Medittext.gettext (). toString (); LOG.E (TAG,"Start onpause~~~"); } @Overrideprotected voidOnStop () {super.onstop (); LOG.E (TAG,"Start onstop~~~"); } @Overrideprotected voidOnDestroy () {Super.ondestroy (); LOG.E (TAG,"Start ondestroy~~~"); }                }  

Seventh step: Rerun the Activitydemo program, repeat the fifth step, when we press the home key, when the application is launched again, EditText has the last input "Frankie" word, as shown:

OK, the basic Gaocheng, this time everyone can look at the activity life cycle diagram, I think we should fully understand the activity life cycle, do not know you know?

Two minutes to fully understand the Android activity life cycle (graphics)! Reproduced

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.