Activity的生命周期

Android应用程序的不能自己控制他们自己的进程,但是Android运行时可以管理每个进程。因此正确的理解Activity的生存期,对于保证用户有个良好的体验有着很大的帮助。

1. Activity栈及状态

Activity栈是当前正在运行的所有Activity后进先出的集合。新启动的Activity始终在栈顶。Activity有四种种状态。

  • 活动状态   当Activity处于栈顶,也就是Activity是可见的,具有焦点的Activity,此时可以接受用户输入。正常情况下,Android Runtime会极力保持该Activity不被终止。
  • 暂停状态  此时的Activity是可见的,但是却是没有焦点的。当一个透明的或者一个非全屏的Activity位于当前Activity时,此时Activity就是处于暂停状态。此状态的Activity近似活动状态。若非内存需要,一般情况下是不会被Runtime终止。
  • 停止状态   当一个Activity处于停止状态时,它就是不可见的。但是它仍然在内存当中,保存当前Activity的状态。当其他地方需要内存时,这种状态的Activity是作为首选的终止的对象。
  • 非活动状态  Activity被终止之后就是处于非活动状态。此状态的Activity已经被移除Activity栈。若要显示此状态的Activity,需要重新启动该Activity。

2.Activity的生命周期

Activity的生命周期图如下所示。

3.Activity生命周期示例。

(1)新建MainActivity类,重写整个生命周期的函数。

MainActivity.java

  1 package com.example.zhouy.androidexercise;
  2 
  3 import android.app.Activity;
  4 import android.content.DialogInterface;
  5 import android.content.Intent;
  6 import android.os.Bundle;
  7 import android.support.v7.app.AlertDialog;
  8 import android.util.Log;
  9 import android.view.View;
 10 import android.widget.Button;
 11 
 12 public class MainActivity extends Activity implements View.OnClickListener{
 13 
 14     private static final String TAG = "Tag";
 15 
 16     private Button mAlertDialogBtn = null;
 17 
 18     private Button mAlertActivityDialogBtn = null;
 19 
 20     private Button mGotoNextActivityBtn = null;
 21 
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         Log.v(TAG,"onCreate()");
 26         setContentView(R.layout.activity_main);
 27         initViews();
 28     }
 29 
 30     private void initViews(){
 31         mAlertDialogBtn = (Button)findViewById(R.id.alert_dialog);
 32         mAlertDialogBtn.setOnClickListener(this);
 33         mAlertActivityDialogBtn = (Button)findViewById(R.id.alert_activtiy_dialog);
 34         mAlertActivityDialogBtn.setOnClickListener(this);
 35         mGotoNextActivityBtn = (Button)findViewById(R.id.go_to_next_activity);
 36         mGotoNextActivityBtn.setOnClickListener(this);
 37     }
 38 
 39     @Override
 40     public void onClick(View view) {
 41         switch (view.getId()){
 42             case R.id.alert_dialog:
 43                 alertDialog();
 44                 break;
 45             case R.id.alert_activtiy_dialog:
 46                 alertDialogActivity();
 47                 break;
 48             case R.id.go_to_next_activity:
 49                 gotoActivity();
 50                 break;
 51         }
 52     }
 53 
 54     private void alertDialog(){
 55         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 56         builder.setTitle("AlertDialog");
 57         builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
 58             @Override
 59             public void onClick(DialogInterface dialogInterface, int i) {
 60                 Log.v(TAG,"点击OK");
 61             }
 62         });
 63         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
 64             @Override
 65             public void onClick(DialogInterface dialogInterface, int i) {
 66                 Log.v(TAG,"点击Cancel");
 67             }
 68         });
 69         builder.setMessage("当前是AlertDialog的弹窗");
 70         builder.create();
 71         builder.show();
 72 
 73     }
 74 
 75     private void alertDialogActivity(){
 76         Intent intent = new Intent(this,DialogActivity.class);
 77         startActivity(intent);
 78     }
 79 
 80     private void gotoActivity(){
 81         Intent intent = new Intent(this,NextActivity.class);
 82         startActivity(intent);
 83     }
 84 
 85     @Override
 86     protected void onRestart() {
 87         super.onRestart();
 88         Log.v(TAG,"onReStart()");
 89     }
 90 
 91     @Override
 92     protected void onStart() {
 93         super.onStart();
 94         Log.v(TAG,"onStart()");
 95     }
 96 
 97     @Override
 98     protected void onResume() {
 99         super.onResume();
100         Log.v(TAG,"onResume()");
101     }
102 
103     @Override
104     protected void onPause() {
105         super.onPause();
106         Log.v(TAG,"onPause()");
107     }
108 
109     @Override
110     protected void onStop() {
111         super.onStop();
112         Log.v(TAG,"onStop");
113     }
114 
115     @Override
116     protected void onDestroy() {
117         super.onDestroy();
118         Log.v(TAG,"onDestroy()");
119     }
120 }

布局文件activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.example.zhouy.androidexercise.MainActivity">
12 
13     <Button
14         android:id="@+id/alert_dialog"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="弹出AlertDialog"/>
18     <Button
19         android:id="@+id/alert_activtiy_dialog"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="弹出Activity的对话框"/>
23     <Button
24         android:id="@+id/go_to_next_activity"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:text="跳转到下一个Activity"/>
28 </LinearLayout>

当App启动后,对照一下上图,运行结果如下图:

清空日志,点击home键,运行结果如下图:

然后再次通过后台返回到该Activity,运行结果为:

通过back键退出Activity,运行结果为:

(2)新建两个Activity,分别是窗口Activity,和一个全屏的Activity。这两个Activity都比较简单。布局都只有一个TextView,不再给出代码,只是对于DialogActivity需要在Manifest.xml的主题声明为Dialog

1 <activity android:name=".DialogActivity"
2        android:theme="@android:style/Theme.Dialog"/>

点击AlertDialog按钮,如下图所示

运行结果:

可以看到Dialog弹出时,对Activity的状态并没有影响,当是DialogActivity弹窗时,MainActivity还是处于暂停生存期。看下个例子。

输出结果:

当进入下一个Activity时,MainActivity是处于停止生存期中。看运行结果如下图。

 

posted @ 2016-07-19 22:59  指尖上的代码  阅读(1002)  评论(0)    收藏  举报