今天我们来了解Android中Activity的生命周期。
Activity有三种状态,分别是运行状态、暂停状态以及停止状态。
运行状态:
当Activity在屏幕的最前端(位于当前堆栈的顶部),它是可见的、有焦点的。可以用来进行处理用户的操作(点击、双击、长按等),那么就叫做激活或运行状态。值得注意的是,当Activity处于运行状态的时候,Android会尽可能的保持它的运行,即使出现内存不足等情况,Android也会先杀死堆栈底部的Activity,来确保运行状态的Activity正常运行。
暂停状态:
在某些情况下,Activity对用户来说,仍然是可见的,但不再拥有焦点,即用户对它的操作是没用实际意义的。在这个时候,它就是属于暂停状态。例如:当最前端的Activity是个透明或者没有全屏,那么下层仍然可见的Activity就是暂停状态。暂停的Activity仍然是激活的(它保留着所有的状态和成员信息并保持与Activity管理器的连接),但当内存不足时,可能会被杀死。
停止状态:
当Activity完全不可见时,它就处于停止状态。它仍然保留着当前状态和成员信息。然而这些对用户来说,都是不可见的;同暂停状态一样,当系统其他地方需要内存时,它也有被杀死的可能。
生命周期事件:
Activity状态的变化是人为操作的,而这些状态的改变,也会触发一些事件。我们且叫它生命周期事件。一共有7个。
下面附一张Activity的七种状态图:
这七种状态分别为:
void onCreate(BundlesavedInstanceState)
Activity的初始化,即创建一个新的Activity
void onStart()
Activity的启动,即一个Activity的开始
void onRestart()
Activity从停止状态,到重新启动的状态
void onResume()
一个Activity开启后,执行一个操作,后进入的状态
void onPause()
一个Activity运行时,另一个Activity突然运行,原Activity将处于暂停状态
void onStop()
Activity处于停止状态,此时若调用void onRestart(),此Activity将重新启动,若调用void onDestroy(),此Activity将被销毁
void onDestroy()
Activity被销毁的状态,此时的Activity将终止
下面结合一个一个实例来了解Activity的生命周期:
这里,先创建布局文件,用到了三个布局文件:
1.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="前进" />
<Button
android:id="@+id/go_to"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show MyDialogActivity" />
<Button
android:id="@+id/go_to_qu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show a real dialog" />
</LinearLayout>
2.second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout>
3.dialog_activity.xml(这个布局文件是对比于真实对话框的一个Activity布局文件)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是对话狂,其实我是Activity" />
</LinearLayout>
其次,则是Activity.java文件:
package cn.class3g.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class ActivityLifeCycleActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "lifeCycle";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "onCreate");
Button goBtn = (Button) this.findViewById(R.id.go);
Button go_toBtn = (Button) this.findViewById(R.id.go_to);
Button go_to_quBtn = (Button) this.findViewById(R.id.go_to_qu);
goBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ActivityLifeCycleActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
go_toBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ActivityLifeCycleActivity.this,
DialogActivity.class);
startActivity(intent);
}
});
go_to_quBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG,"showAlerDialog");
showAlertDialog();
}
});
}
public void showAlertDialog() {
AlertDialog.Builder builder = new Builder(
ActivityLifeCycleActivity.this);
builder.setMessage("确认退出吗?");
builder.setTitle("提示");
builder.setPositiveButton("确定",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ActivityLifeCycleActivity.this.finish();
}
});
builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//创建并显示
builder.create().show();
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"onRestoreInstanceState()");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG,"onSaveInstanceState()");
}
}
启动模拟机,进行各种点击操作,实现各个状态之间的转换从而,得出如下小结:
1、运行activity
onCreate -- onStart -- onReaume
2、点击返回键
onPause -- onStop -- onDestory
-----------------
1、运行
2、点击Home
onPause--onStop
3、在桌面,长按Home重新回到ActivityLifeCycle
onRestart -- onStart -- onResume
---------------------
1、运行
2、切换至SecondActivity之上
onPause -- onStop
3、切换回AcitivityLifeCycle
onCreate -- onStart -- onResume
4、点击返回键时
onRestart -- onStart -- onResume
-----------------------
创建一个对话框样式的activity:DialogActivity
1、运行
2、切换至DoalogActivity
onPause
3、按返回键返回至ActivityLifeCycle
onResume
--------------------
创建AlertDialog对话框
显示对话框并返回,发现此时并不能触发ActivityLifeCycle
实例的状态变化,其生命周期中任何函数
-------------------------
添加
protected voidonRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"OnRestoreInstanceState()");
}
protected void onSaveInstanceState(BundleoutState){
super.onRestoreInstanceState(outState);
Log.i(TAG,"OnSaveInstanceState()");
}
1、运行程序
2、按Home
onSaveInstanceState—— onPause —— OnStop
3、在桌面,长按Home重新回到ActivityLifeCyde
onRestart ——onStart —— onResume
注意:此时并没用触发onRestoreInstanceState()