AlertDialog,Toast對Activity生命週期的影響

轉自: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在服務中使用

[java] view plain copy
  1. Handler handler = new Handler(Looper.getMainLooper());  
  2. handler.post(new Runnable() {  
  3.     @Override  
  4.     public void run() {  
  5.         showAlertDialog();  
  6.     }  
  7. });  
跨進程彈出對話框,實際證明也是沒有影響的。)

通過官方文檔我們可以看到: 

    

onPause() Called when the system is about to start resuming another activity. 
只有再啓動另外一個Activity的時候纔會進入onPause狀態,而不是想象中的被覆蓋或者不可見

同時通過AlertDialog源碼或者Toast源碼我們都可以發現它們實現的原理都是windowmanager.addView();來添加的, 它們都是一個個view ,因此不會對activity的生命週期有任何影響。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章