android illegal exception when dialog is showing

A simple way to safely dismiss your dialog is to check if the view the Dialog is contained within is currently showing before attempting to dismiss it. This looks like:

if (view.isShown()) {
    dialog.dismiss()
}

The top level of your stacktrace is telling you what is wrong:

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{21f9ba68 V.E..... R.....ID 0,0-1136,402} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:328)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:433)
at android.app.Dialog.dismiss(Dialog.java:416)

You are calling dismiss on a dialog that is currently not being shown anymore. As in: your Activity/Fragment is possibly already destroyed when you call dismiss (-> "not attached to window manager").

[edit] One way to fix this is to check for activity.isFinishing() or fragment.isAdded()


Please dismiss as follows

if ((alertDialog != null) && alertDialog.isShowing())
        {
            alertDialog.dismiss();
        }