转自:http://blog.csdn.net/scorpioneal/article/details/19049475
经常可以在网上看到一些文章介绍Activity生命周期, 说只要一个Activity被覆盖,不是完全可见, 那么它就处于onPause状态或者不可见, 则处于onStop状态, 之前自己也是一直这样以为, 知道后来碰到一些情况(toast的弹出, AlertDialog的弹出等) 才发现并不是这样。
很简单的做个试验: 点击按钮弹出一个AlertDialog, 这时, 后面的Activity处于不完全可见的状态, 打印出Activity生命周期的变化。
- package com.example.myandroiddemo;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
-
- public class MainActivity extends Activity {
-
- private static final String TAG = MainActivity.class.getSimpleName();
- private Context mContext = MainActivity.this;
- private Button mButton;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d(TAG, "onCreate");
-
- mButton = (Button) findViewById(R.id.clickme);
- mButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setTitle("This is a sample");
- builder.create();
- builder.show();
- }
- });
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d(TAG, "onDestroy");
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- Log.d(TAG, "onPause");
- }
-
- @Override
- protected void onRestart() {
- super.onRestart();
- Log.d(TAG, "onRestart");
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(TAG, "onResume");
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- Log.d(TAG, "onStart");
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- Log.d(TAG, "onStop");
- }
-
- }
这时我们发现在打印完onCreate() onStart() onResume()之后 点击按钮弹出对话框, 这时并没有其他log打印, 界面显示如下

这表明弹出的东西对后面Activity的生命周期并没有影响, 有人可能会觉得是因为弹出的东西太小了。 我们修改代码如下:
- package com.example.myandroiddemo;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
-
- public class MainActivity extends Activity {
-
- private static final String TAG = MainActivity.class.getSimpleName();
- private Context mContext = MainActivity.this;
- private Button mButton;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d(TAG, "onCreate");
-
- mButton = (Button) findViewById(R.id.clickme);
- mButton.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setView(new MyView(mContext));
- builder.setTitle("This is a sample");
- builder.create();
- builder.show();
- }
- });
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d(TAG, "onDestroy");
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- Log.d(TAG, "onPause");
- }
-
- @Override
- protected void onRestart() {
- super.onRestart();
- Log.d(TAG, "onRestart");
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- Log.d(TAG, "onResume");
- }
-
- @Override
- protected void onStart() {
- super.onStart();
- Log.d(TAG, "onStart");
- }
-
- @Override
- protected void onStop() {
- super.onStop();
- Log.d(TAG, "onStop");
- }
-
- class MyView extends RelativeLayout{
-
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- LayoutInflater.from(mContext).inflate(R.layout.tmp_view, this, true);
- }
-
- public MyView(Context context) {
- this(context, null);
- }
-
- }
-
- }
其中的布局是:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:background="#663333"
- android:layout_height="match_parent" >
- </RelativeLayout>
显示的结果:

设置的是全屏的一个有颜色的RelativeLayout(至于为什么显示没有显示为全屏, 稍后再探讨)
同样,对于后面的Activity生命周期没有任何影响。
注:(基于原文作者的基础上,我看到网上也有人问alertdialog对activity生命周期的影响,
底下有问答:所弹alertDialog对自身应用不产生影响,但如果弹出时是其他应用的activity在运行则会有影响,
于是笔者基于aidl在服务中使用
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
showAlertDialog();
}
});
跨进程弹出对话框,实际证明也是没有影响的。)
通过官方文档我们可以看到:
onPause()
| Called when the system is about to start resuming another activity. |
只有再启动另外一个Activity的时候才会进入onPause状态,而不是想象中的被覆盖或者不可见
同时通过AlertDialog源码或者Toast源码我们都可以发现它们实现的原理都是windowmanager.addView();来添加的, 它们都是一个个view ,因此不会对activity的生命周期有任何影响。