Android dialog 问题

1.dialog.dismiss和dialog.cancel的区别

Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered).

取消对话框,基本上和调用dismiss效果一样。但是cancel同事也会调用DialogInterface.OnCancelListener注册的事件,如果注册了。

Dismiss this dialog, removing it from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().

dismiss是线程安全的。

2.但使用dismiss的时候,会遇到一些问题,下面来罗列一下这些问题以及解决方法。

比如在界面上显示一个Dialog,当任务处理结束后再Dismiss Dialog。如果在Dialog显示期间,该Activity因为某种原因被杀掉且又重新启动了,那么当任务结束时,Dismiss Dialog的时候WindowManager检查,就会发现该Dialog所属的Activity已经不存在了(重新启动了一次,是一个新的Activity),所以会报IllegalArgumentException: View not attached to window manager.

当我们的Dialog还没有dismiss时,如果此时该Activity被销毁了,那么就会出现以上错误,提示窗口泄漏(leaked window)。

出现这个异常的原因可能是,在dismiss对话框的时候,它所在的activity因为一些原因已经先退出了,所以会出现这个异常。 

附加一个网址,里面有一些我自己认为很不错的android的问题以及解决方法

http://www.2cto.com/kf/201502/375068.html

<a> 两种方法 <1>.

目前我认为最好的解决方法是,使用Activity里面封装好的showDialog(int id)和dismissDialog(int id)方法。

使用Activity自带的Dialog控制方法

       在Activity中需要使用对话框,可以使用Activity自带的回调,比如onCreateDialog(),showDialog(),dimissDialog(),removeDialog()等等。毕竟这些都是Activity自带的方法,所以用起来更方便,也不用显示创建和操控Dialog对象,一切都由框架操控,相对来说比较安全。

如何使用,下面是代码demo

  1. package com.sinaapp.msdxblog.android.lol.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Dialog;  
  5. import android.app.ProgressDialog;  
  6. import android.content.Context;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.webkit.WebView;  
  10. import android.webkit.WebViewClient;  
  11. import android.widget.FrameLayout;  
  12. import android.widget.FrameLayout.LayoutParams;  
  13. import android.widget.LinearLayout;  
  14.   
  15. import com.sinaapp.msdxblog.android.lol.R;  
  16. import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;  
  17.   
  18. /** 
  19.  * @author Geek_Soledad (66704238@51uc.com) 
  20.  */  
  21. public abstract class WebViewActivity extends Activity {  
  22.   
  23.     protected WebView mSearchWV;  
  24.     protected Context mContext;  
  25.     private static final int PROGRESS_ID = 1;  
  26.   
  27.     /** 
  28.      * 返回需要加载的URL地址。 
  29.      *  
  30.      * @return 需要加载的URL地址。 
  31.      */  
  32.     protected abstract String getHomeUrl();  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         mContext = this;  
  38.         mSearchWV = new WebView(mContext);  
  39.         mSearchWV.getSettings().setJavaScriptEnabled(true);  
  40.         mSearchWV.setWebViewClient(new WebViewClient() {  
  41.   
  42.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  43.                 view.loadUrl(url);  
  44.                 return true;  
  45.             }  
  46.   
  47.             @Override  
  48.             public void onPageStarted(WebView view, String url, Bitmap favicon) {  
  49.                 super.onPageStarted(view, url, favicon);  
  50.                 showDialog(PROGRESS_ID);  
  51.             }  
  52.   
  53.             @Override  
  54.             public void onPageFinished(WebView view, String url) {  
  55.                 super.onPageFinished(view, url);  
  56.                 dismissDialog(PROGRESS_ID);  
  57.             }  
  58.         });  
  59.         addContentView(mSearchWV, new FrameLayout.LayoutParams(  
  60.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  61.   
  62.         mSearchWV.loadUrl(getHomeUrl());  
  63.     }  
  64.   
  65.     @Override  
  66.     protected Dialog onCreateDialog(int id) {  
  67.         if (id == PROGRESS_ID) {  
  68.             return ProgressDialog.show(mContext, null,  
  69.                     mContext.getString(R.string.loading));  
  70.         }  
  71.         return super.onCreateDialog(id);  
  72.     }  
  73. }  

<2>

限制Dialog的生命周期

       让创建的Dialog对象的存活周期跟Activity的生命周期一致,也就是说Dialog的生命周期被限定在Activity的onCreate()和onDestroy()方法之间。

  1. private void closeDialog() {  
  2.     if (mDialog != null && mDialog.isShowing()) {  
  3.         mDialog.dismiss();  
  4.     }  
  5. }  
  6.   
  7. @Override  
  8. protected void onDestroy() {  
  9.     Log.d(TAG, "called onDestroy");  
  10.     mIsDestroyed = true;  
  11.     closeDialog();  //在这里也可以换成dialog=null
  12.     super.onDestroy();  
  13. }

<b> 下面是一种高大上的解决方法,在源码的基础上。

项目里的ProgressDialog导致了这么一个IllegalArgumentException异常,原因是在延时线程里调用了ProgressDialog.dismiss,但此时主Activity已经destroy了。于是应用崩溃,我写了一个 SafeProgressDialog 来避免这个问题,主要原理是覆写dismiss方法,在ProgressDialog.dismiss之前判断Activity是否存在。

下面是一个自定义的SafeProgressDialog,可以完美地防止IllegalArgumentException。

class SafeProgressDialog extends ProgressDialog

{

    Activity mParentActivity;

    public SafeProgressDialog(Context context)

    {

        super(context);

        mParentActivity = (Activity) context;

    }

 

    @Override

    public void dismiss()

    {

        if (mParentActivity != null && !mParentActivity.isFinishing())

        {

            super.dismiss();    //调用超类对应方法

        }

    }

}

下面是测试demo的代码

package com.hankcs.LoadingProgressDialogTest;

 

import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Context;

import android.os.Bundle;

import android.os.Handler;

 

public class MyActivity extends Activity

{

 

    private ProgressDialog mProgressDialog;

 

    /**

     * Called when the activity is first created.

     */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        // The code below throws a java.lang.IllegalArgumentException: View not attached to window manager

        // Which makes your app crashed

//        mProgressDialog = new ProgressDialog(this);

        // But my SafeProgressDialog will solve this issue

        mProgressDialog = new SafeProgressDialog(this);

        mProgressDialog.show();

        new Handler().postDelayed(new Runnable()

        {

            @Override

            public void run()

            {

                mProgressDialog.dismiss();

            }

        }, 1000);

 

        finish();

    }

}

 

class SafeProgressDialog extends ProgressDialog

{

    Activity mParentActivity;

    public SafeProgressDialog(Context context)

    {

        super(context);

        mParentActivity = (Activity) context;

    }

 

    @Override

    public void dismiss()

    {

        if (mParentActivity != null && !mParentActivity.isFinishing())

        {

            super.dismiss();    //调用超类对应方法

        }

    }

}

下面贴一个安卓实战技巧dialog的 http://www.doc88.com/p-772890177672.html

http://blog.csdn.net/hitlion2008/article/details/7567549

 

 

原文地址:https://www.cnblogs.com/yaya-Android/p/4506307.html