AlertDialog,Toast对Activity生命周期的影响

来源:互联网 发布:sql怎删查改 编辑:程序博客网 时间:2024/04/29 23:13

转自:http://blog.csdn.net/scorpioneal/article/details/19049475


经常可以在网上看到一些文章介绍Activity生命周期, 说只要一个Activity被覆盖,不是完全可见, 那么它就处于onPause状态或者不可见, 则处于onStop状态, 之前自己也是一直这样以为, 知道后来碰到一些情况(toast的弹出, AlertDialog的弹出等) 才发现并不是这样。

     很简单的做个试验: 点击按钮弹出一个AlertDialog, 这时, 后面的Activity处于不完全可见的状态, 打印出Activity生命周期的变化。

     

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.myandroiddemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.os.Bundle;  
  7. import android.util.AttributeSet;  
  8. import android.util.Log;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.RelativeLayout;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private static final String TAG = MainActivity.class.getSimpleName();  
  18.     private Context mContext = MainActivity.this;  
  19.     private Button mButton;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         Log.d(TAG, "onCreate");  
  26.   
  27.         mButton = (Button) findViewById(R.id.clickme);  
  28.         mButton.setOnClickListener(new View.OnClickListener() {  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);  
  33.                 builder.setTitle("This is a sample");  
  34.                 builder.create();  
  35.                 builder.show();  
  36.             }  
  37.         });  
  38.     }  
  39.   
  40.     @Override  
  41.     protected void onDestroy() {  
  42.         super.onDestroy();  
  43.         Log.d(TAG, "onDestroy");  
  44.     }  
  45.   
  46.     @Override  
  47.     protected void onPause() {  
  48.         super.onPause();  
  49.         Log.d(TAG, "onPause");  
  50.     }  
  51.   
  52.     @Override  
  53.     protected void onRestart() {  
  54.         super.onRestart();  
  55.         Log.d(TAG, "onRestart");  
  56.     }  
  57.   
  58.     @Override  
  59.     protected void onResume() {  
  60.         super.onResume();  
  61.         Log.d(TAG, "onResume");  
  62.     }  
  63.   
  64.     @Override  
  65.     protected void onStart() {  
  66.         super.onStart();  
  67.         Log.d(TAG, "onStart");  
  68.     }  
  69.   
  70.     @Override  
  71.     protected void onStop() {  
  72.         super.onStop();  
  73.         Log.d(TAG, "onStop");  
  74.     }  
  75.       
  76. }  

这时我们发现在打印完onCreate() onStart() onResume()之后 点击按钮弹出对话框, 这时并没有其他log打印, 界面显示如下


这表明弹出的东西对后面Activity的生命周期并没有影响, 有人可能会觉得是因为弹出的东西太小了。 我们修改代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.myandroiddemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.Context;  
  6. import android.os.Bundle;  
  7. import android.util.AttributeSet;  
  8. import android.util.Log;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.RelativeLayout;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     private static final String TAG = MainActivity.class.getSimpleName();  
  18.     private Context mContext = MainActivity.this;  
  19.     private Button mButton;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         Log.d(TAG, "onCreate");  
  26.   
  27.         mButton = (Button) findViewById(R.id.clickme);  
  28.         mButton.setOnClickListener(new View.OnClickListener() {  
  29.   
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);  
  33.                 builder.setView(new MyView(mContext));  
  34.                 builder.setTitle("This is a sample");  
  35.                 builder.create();  
  36.                 builder.show();  
  37.             }  
  38.         });  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onDestroy() {  
  43.         super.onDestroy();  
  44.         Log.d(TAG, "onDestroy");  
  45.     }  
  46.   
  47.     @Override  
  48.     protected void onPause() {  
  49.         super.onPause();  
  50.         Log.d(TAG, "onPause");  
  51.     }  
  52.   
  53.     @Override  
  54.     protected void onRestart() {  
  55.         super.onRestart();  
  56.         Log.d(TAG, "onRestart");  
  57.     }  
  58.   
  59.     @Override  
  60.     protected void onResume() {  
  61.         super.onResume();  
  62.         Log.d(TAG, "onResume");  
  63.     }  
  64.   
  65.     @Override  
  66.     protected void onStart() {  
  67.         super.onStart();  
  68.         Log.d(TAG, "onStart");  
  69.     }  
  70.   
  71.     @Override  
  72.     protected void onStop() {  
  73.         super.onStop();  
  74.         Log.d(TAG, "onStop");  
  75.     }  
  76.       
  77.     class MyView extends RelativeLayout{  
  78.   
  79.         public MyView(Context context, AttributeSet attrs) {  
  80.             super(context, attrs);  
  81.             LayoutInflater.from(mContext).inflate(R.layout.tmp_view, thistrue);  
  82.         }  
  83.   
  84.         public MyView(Context context) {  
  85.             this(context, null);  
  86.         }  
  87.           
  88.     }  
  89.   
  90. }  

其中的布局是: 

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:background="#663333"  
  5.     android:layout_height="match_parent" >  
  6. </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的生命周期有任何影响。 

2 0