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:
?
1234567891011121314151617 |
<?
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:
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
package com.tutor.activitydemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
public class ActivityDemo
extends Activity {
private static final String TAG =
"ActivityDemo"
;
private EditText mEditText;
//定义一个String 类型用来存取我们EditText输入的值
private String mString;
public void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEditText = (EditText)findViewById(R.id.editText);
Log.e(TAG,
"start onCreate~~~"
);
}
@Override
protected void onStart() {
super
.onStart();
Log.e(TAG,
"start onStart~~~"
);
}
//当按HOME键时,然后再次启动应用时,我们要恢复先前状态
@Override
protected void onRestart() {
super
.onRestart();
mEditText.setText(mString);
Log.e(TAG,
"start onRestart~~~"
);
}
@Override
protected void onResume() {
super
.onResume();
Log.e(TAG,
"start onResume~~~"
);
}
//当我们按HOME键时,我在onPause方法里,将输入的值赋给mString
@Override
protected void onPause() {
super
.onPause();
mString = mEditText.getText().toString();
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~~~"
);
}
}
|
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?
Translated from: http://blog.csdn.net/android_tutor/article/details/5772285
Two minutes to fully understand the Android activity life cycle (graphics)!