Android api中提供的Activity生命周期图
创建一个测试项目ActivityLife
在MainActivity.java中
重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法
package com.example.activitylife;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "~~~onCreate~~~");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "~~~onStart~~~");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "~~~onReStart~~~");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "~~~onResume~~~");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "~~~onPause~~~");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "~~~onStop~~~");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "~~~onDestroy~~~");
}
}
运行该应用程序
下面是不同操作对应的LogCat输出
打开应用程序时,对应下图过程1
![](https://i-blog.csdnimg.cn/blog_migrate/dbe5122caf5f1c20899284843ded6f53.png)
打开应用程序后,按下BACK键时,对应下图过程2
![](https://i-blog.csdnimg.cn/blog_migrate/f099dbc3ddd6bc513f1b4b5ef5e9847d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/64b2ae0f24ff9168adc2f5763b153c99.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9763097a649bf29a408947e1a1ccdf86.png)
========================================================================
![](https://i-blog.csdnimg.cn/blog_migrate/f099dbc3ddd6bc513f1b4b5ef5e9847d.png)
打开应用程序后,按下HOME键时,对应下图过程3
![](https://i-blog.csdnimg.cn/blog_migrate/64b2ae0f24ff9168adc2f5763b153c99.png)
在上一操作的基础上,再次
打开
应用程序时,对应下图过程4
![](https://i-blog.csdnimg.cn/blog_migrate/9763097a649bf29a408947e1a1ccdf86.png)
操作流程图:
![](https://i-blog.csdnimg.cn/blog_migrate/c31e9b78c44c5f2ccd896153059a6e2f.png)
========================================================================
参考文章提出的一个问题:
EditText在状态1时输入一个字符串,如"Hello,Android!",再经过3,4状态后无法保持
看到有评论说 有id的view,android会自动维护状态
经测试,无论EditText无论有无id,字符串都可保持住
测试如下:
修改res/layout/activity_main.xml,在布局中添加一个EditText控件
<? xml version ="1.0" encoding= "utf-8" ?> < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:orientation ="vertical" android:paddingBottom ="@dimen/activity_vertical_margin" android:paddingLeft ="@dimen/activity_horizontal_margin" android:paddingRight ="@dimen/activity_horizontal_margin" android:paddingTop ="@dimen/activity_vertical_margin" tools:context =".MainActivity" > <TextView android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="@string/hello_world" /> <EditText android:id ="@+id/editText" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:inputType ="text" /> </ LinearLayout>
再依次运行过程1-->3-->4
在状态1的时候,在EditText控件中写入一个字符串,如Hello,Android!,在经过3,4过程后字符串依旧保持
========================================================================
![](https://i-blog.csdnimg.cn/blog_migrate/8f9ada97ee2298df4923692756def572.png)
========================================================================
测试状态5
修改res/layout/main_activity.xml,添加一个Button
<? xml version ="1.0" encoding= "utf-8" ?> < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:orientation ="vertical" android:paddingBottom ="@dimen/activity_vertical_margin" android:paddingLeft ="@dimen/activity_horizontal_margin" android:paddingRight ="@dimen/activity_horizontal_margin" android:paddingTop ="@dimen/activity_vertical_margin" tools:context =".MainActivity" > <TextView android:id = "@+id/mainTv" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="@string/hello_world" /> <EditText android:id = "@+id/mainEt" android:layout_width= "fill_parent" android:layout_height ="wrap_content" android:inputType ="text" /> <Button android:id ="@+id/callDialog" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="@string/call_dialog" /> </ LinearLayout>
添加一个Activity,SecondActivity
并重写onCreate,onStart,onResume,onPause,onStop,onRestart,onDestroy方法
package com.example.activitylife;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity {
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.e(TAG, "~~~onCreate~~~");
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "~~~onStart~~~");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "~~~onReStart~~~");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "~~~onResume~~~");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "~~~onPause~~~");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "~~~onStop~~~");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "~~~onDestroy~~~");
}
}
修改MainActivity.java
package com.example.activitylife;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Button callDialogBut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "~~~onCreate~~~");
callDialogBut = (Button) findViewById(R.id.callDialog);
callDialogBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "~~~onStart~~~");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e(TAG, "~~~onReStart~~~");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "~~~onResume~~~");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "~~~onPause~~~");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "~~~onStop~~~");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "~~~onDestroy~~~");
}
}
修改AndroidManifest.xml文件
<? xml version ="1.0" encoding= "utf-8" ?> < manifest xmlns:android ="http://schemas.android.com/apk/res/android" package ="com.example.activitylife" android:versionCode ="1" android:versionName ="1.0" > <uses-sdk android:minSdkVersion ="8" android:targetSdkVersion ="17" /> <application android:allowBackup ="true" android:icon ="@drawable/ic_launcher" android:label ="@string/app_name" android:theme ="@style/AppTheme" > < activity android:name ="com.example.activitylife.MainActivity" android:label ="@string/app_name" > < intent-filter> < action android:name ="android.intent.action.MAIN" /> < category android:name ="android.intent.category.LAUNCHER" /> </ intent-filter> </ activity> < activity android:name =".SecondActivity" android:label ="@string/app_name" android:theme ="@android:style/Theme.Dialog" > </ activity> </application > </ manifest>
启动应用程序后,点击 调用对话框 按钮,相当于过程5中的onPause
![](https://i-blog.csdnimg.cn/blog_migrate/b4039d755385c55b5ca0abfa875e1bb3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b4039d755385c55b5ca0abfa875e1bb3.png)
在上一步操作的基础上,点击BACK键,相当于过程5中的onResume
![](https://i-blog.csdnimg.cn/blog_migrate/a15cd7dc297902ce14d176fa1b0ef456.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a15cd7dc297902ce14d176fa1b0ef456.png)
程序运行截图:
启动应用程序
![](https://i-blog.csdnimg.cn/blog_migrate/edd02163f1afd12c88c3a6ddc6ca48d4.png)
按下 调用对话框 按钮
![](https://i-blog.csdnimg.cn/blog_migrate/7ccd7c9002c89dbdeebe762bbdb43842.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7ccd7c9002c89dbdeebe762bbdb43842.png)
再按下BACKUP键
![](https://i-blog.csdnimg.cn/blog_migrate/f2824273db4dd14105298aec962c484c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f2824273db4dd14105298aec962c484c.png)
参考文章:
-----------------------