轉自: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在服務中使用
跨進程彈出對話框,實際證明也是沒有影響的。)
通過官方文檔我們可以看到:
|
Called when the system is about to start resuming another activity. |
同時通過AlertDialog源碼或者Toast源碼我們都可以發現它們實現的原理都是windowmanager.addView();來添加的, 它們都是一個個view ,因此不會對activity的生命週期有任何影響。