学习并掌握Activity生命周期,对从事Android开发(或者打算日后从事这方面的开发工作)的朋友来讲,是至关重要的。本文将用图解和实例的方式,向大家详细讲解Activity生命周期的有关知识。
Activity有三个状态:

当Activity从一种状态转变到另一种状态时,会调用以下保护方法来通知这种变化:
void onCreate(Bundle savedInstanceState)
void onStart()
void onRestart()
void onResume()
void onPause()
void onStop()
void onDestroy()
这七个方法定义了Activity的完整生命周期。实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环:
Activity的前台生命周期自onResume()调用起,至相应的onPause()调用为止。在此期间,Activity位于前台最上面并与用户进行交互。Activity会经常在暂停和恢复之间进行状态转换——例如当设备转入休眠状态或者有新的Activity启动时,将调用onPause() 方法。当Activity获得结果或者接收到新的Intent时会调用onResume() 方法。
说了一大堆的理论,下面一起动手来开发一个小实例,帮助大家快速理解。不妨试着动手去敲代码,然后自己监控控制台上的日志记录。
先贴出Activity部分的代码:
最先是一个MainActivity,这个也是软件启动时默认打开的。
- packagecn.itcast.life;
- importandroid.app.Activity;
- importandroid.content.Intent;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.widget.Button;
- publicclassMainActivityextendsActivity{
- privatestaticfinalStringTAG="MainActivity";
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.i(TAG,"onCreate()");
- Buttonbutton=(Button)this.findViewById(R.id.button);
- button.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Intentintent=newIntent(MainActivity.this,OtherActivity.class);
- startActivity(intent);
- }
- });
- Buttonthreebutton=(Button)this.findViewById(R.id.threebutton);
- threebutton.setOnClickListener(newView.OnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- Intentintent=newIntent(MainActivity.this,ThreeActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protectedvoidonDestroy(){
- Log.i(TAG,"onDestroy()");
- super.onDestroy();
- }
- @Override
- protectedvoidonPause(){
- Log.i(TAG,"onPause()");
- super.onPause();
- }
- @Override
- protectedvoidonRestart(){
- Log.i(TAG,"onRestart()");
- super.onRestart();
- }
- @Override
- protectedvoidonResume(){
- Log.i(TAG,"onResume()");
- super.onResume();
- }
- @Override
- protectedvoidonStart(){
- Log.i(TAG,"onStart()");
- super.onStart();
- }
- @Override
- protectedvoidonStop(){
- Log.i(TAG,"onStop()");
- super.onStop();
- }
- }
MainActivity所匹配的xml布局文件:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开OtherActivity"
- android:id="@+id/button"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开ThreeActivity"
- android:id="@+id/threebutton"
- />
- </LinearLayout>
下面将是一个新的Activity,为了验证“onstop”方法,我们要用这个OtherActivity将前面的MainActivity覆盖掉。
- packagecn.itcast.life;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassOtherActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.other);
- }
- }
- <?xmlversion="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="这是OtherActivity"
- />
- </LinearLayout>
还有一个Activity,是用于测试onpause方法的。使用半透明或者提示框的形式,覆盖掉前面的MainActivity
- packagecn.itcast.life;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassThreeActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- //TODOAuto-generatedmethodstub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.three);
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="第三个Activity"
- />
- </LinearLayout>
下面是项目清单文件,注意使用android:theme="@android:style/Theme.Dialog"来设置activity 的样式风格--弹出框
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.itcast.life"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <activityandroid:name=".OtherActivity"android:theme="@android:style/Theme.Dialog"/>
- <activityandroid:name=".ThreeActivity"/>
- </application>
- <uses-sdkandroid:minSdkVersion="8"/>
- </manifest>
在模拟器中部署该应用,观察控制台中的日志显示,看这些生命周期的执行顺序,拭目以待吧。。。。。。
参照那张图片,“当系统内存不足...”的这种情况,我暂时就不去模拟操作了。
补充:
Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate()、onPause()等生命周期方法,它们并不一定会被触发。当应用遇到意外情况(如:内存不足、用户直接按Home键)由系统销毁一个Activity时,onSaveInstanceState()才会被调用。但是当用户主动去销毁一个Activity时,例如在应用中按返回键,onSaveInstanceState()就不会被调用。因为在这种情况下,用户的行为决定了不需要保存Activity的状态。通常onSaveInstanceState()只适合用于保存一些临时性的状态,而onPause()适合用于数据的持久化保存。
提示:在小例子“Android音乐播放器”中,就使用到了该方法。典型的应用场景:比如用户正在听音乐,在这首歌的1:32秒钟时候,突然来电话了(发生意外,音乐播放器的Activity会被电话的Activity覆盖掉),此时音乐会暂停,用户正常的接听电话。等用户接完电话后,音乐播放器会记住当时的记录是1:32,可以自动去继续播放...
转:http://blog.youkuaiyun.com/dinglang_2009/article/details/6890008