四大组件之activity生命周期探索

来源:互联网 发布:奇爱博士 知乎 编辑:程序博客网 时间:2024/06/05 02:22

  在android开发中,activity可以说是我们见过的最多的组件了,我们平常app的界面都是通过activity来展现在我们面前的,可以包含多种用户界面的组件,主要用于和用户进行交互。一个应用程序中可以包含零个或多个活动,但不包含任何活动的应用程序很少见。本篇博文主要是讲解activity的生命周期。
  
  先看一下官网的定义:

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.An activity has essentially four states: - If an activity is in the foreground of the screen (at the top of the stack), it is active or running. - If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. - If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. - If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

  搞android开发的英语应该都还行吧,上面的应该都可以看得懂吧,下面也照顾一下英语不好的人,用我那渣渣英语翻译一下,能看懂的最好看官网的英语说明,那样自己理解更清楚。
  也就是说activity有四个状态:运行,暂停,停止,销毁(ps:摘抄自郭神《第一行代码》,自己总结跟郭神差太远,借来用用)
  
- 运行状态:当一个活动位于返回栈的栈顶时,这时活动就处于运行状态。系统最不愿意回收的就是处于运行状态的活动,因为这会带来非常差的用户体验。
- 暂停状态:当一个活动不再处于栈顶位置,但仍然可见时,这时活动就进入了暂停状态。有的活动不会占满整个屏幕,比如对话框形式的活动,处于暂停状态的活动仍然是完全存活着的,系统也不愿意去回收这种活动(因为它还是可见的,回收可见的东西都会在用户体验方面有不好的影响),只有在内存极低的情况下,系统才会去考虑回收这种活动。
- 停止状态:当一个活动不再处于栈顶位置,并且完全不可见的时候,就进入了停止状态。系统仍然会为这种活动保存相应的状态和成员变量,但是这并不是完全可靠的,当其他地方需要内存时,处于停止状态的活动有可能会被系统回收。
- 销毁状态:当一个活动从返回栈中移除后就变成了销毁状态。系统会最倾向于回收处于这种状态的活动,从而保证手机的内存充足。

四种状态的工作时间大致如下图所示:
这里写图片描述
从图上可以看出,只有调用完OnResume()函数后,activity才运行起来了,也就是我们可见了,activity的完整生命周期就是从OnCreate()到OnDestroy()的过程。
这七个函数的调用流程请看下面图
这里写图片描述

下面我们新建一个项目来探索activity的生命周期

一个MainActivity和一个DialogActivity,怎么建立我就不多说了,代码都蛮简单,直接把xml和java文件贴出来。
activity_main.xml布局文件

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"   >    <Button        android:id="@+id/btnStartDialog"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Start Dialog" /></LinearLayout>

dialog.layout.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" >    <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="This is a dialog activity"/></LinearLayout>

DialogActivity.java代码如下

public class NormalActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.dialog_layout);    }}

我们最主要的是看下面的MainActivity.java代码

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private String TAG = "MainActivity";    private Button btnStartDialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Log.d(TAG, "--->>>onCreate()");        setContentView(R.layout.activity_main);        btnStartDialog = (Button) findViewById(R.id.btnStartDialog);        btnStartDialog.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(getApplicationContext(), NormalActivity.class);                startActivity(intent);            }        });    }    @Override    protected void onDestroy() {        Log.d(TAG, "--->>>OnDestroy()");        super.onDestroy();    }    @Override    protected void onPause() {        Log.d(TAG, "--->>>onPause()");        super.onPause();    }    @Override    protected void onRestart() {        Log.d(TAG, "--->>>onRestart()");        super.onRestart();    }    @Override    protected void onResume() {        Log.d(TAG, "--->>>onResume()");        super.onResume();    }    @Override    protected void onStart() {        Log.d(TAG, "--->>>onStart()");        super.onStart();    }    @Override    protected void onStop() {        Log.d(TAG, "--->>>onStop()");        super.onStop();    }}

这里我在每一个方法里面都打印了一句话,来看啥时候调用了哪个函数
请注意:我们显示调用了父类的实现方法,然后再打印一句日志,这些父类方法的调用不可或缺,另外,猜onCreate()方法里,必须首先调用父类的实现方法,然后再调用其他方法,这一点很关键,而在其他几个方法中,是否首先调用父类方法就不那么重要了。
  记得把打印信息筛选一下,下面运行程序看看效果吧。
  这里写图片描述
可以看到,当MainActivity第一次被创建时会依次执行onCreate()、onStart()和onResume()方法,这时候我们就可以看见界面了,前面也说过,只有等系统调用了onResume()方法后,activity才会显示在我们眼前,一段时间不操作,手机屏幕黑了后(手机处于睡眠状态),
这里写图片描述

当手机不再睡眠后(手机屏幕点亮后)
这里写图片描述

按下大返回键后,程序并没有结束,而是在后台了
这里写图片描述

我们打开后台的这个程序,程序又被唤醒了
这里写图片描述

现在我们点击那个按钮
这里写图片描述
我们看看点击按钮后的日志信息
这里写图片描述
可以看到,只有onPause()方法得到了执行,onStop()方法并没有执行,这是因为DialogActivity并没有完全遮挡住MainActivity,此时MainActivity只是进入了暂停状态,并没有进入停止状态。相应地,按下Back键返回MainActivity也应该只有onResume()方法会得到执行
然后,我们按下返回键
这里写图片描述
最后在MainActivity按下Back键退出程序
这里写图片描述

然后再回过头去看最上面的那两张图,是不是感觉很清晰了,当然,这其中还有数据暂存的问题,下篇博客再介绍。

0 0
原创粉丝点击