Android笔记(5)---Activity生命周期
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

打开应用程序后,按下BACK键时,对应下图过程2

打开应用程序后,按下HOME键时,对应下图过程3

在上一操作的基础上,再次打开应用程序时,对应下图过程4

操作流程图:

========================================================================
参考文章提出的一个问题:
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过程后字符串依旧保持

========================================================================
测试状态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

在上一步操作的基础上,点击BACK键,相当于过程5中的onResume

程序运行截图:
启动应用程序

按下 调用对话框 按钮

再按下BACKUP键

参考文章:
-----------------------