Question

I keep getting this error but I am not sure why

java.lang.IllegalArgumentException: View not attached to window manager
        at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
        at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
        at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
        at android.app.Dialog.dismissDialog(Dialog.java:328)
        at android.app.Dialog$1.run(Dialog.java:119)
        at android.app.Dialog.dismiss(Dialog.java:313)

And it happens in this code

    @Override
    protected void onPostExecute(Void result) {
        if(!isFinishing() && dialog.isShowing()){
            dialog.dismiss();  <-------- HERE IT HAPPENS
        }
   }

Any idea why? I am making sure the activity is not finishing and the dialog is showing before dismissal!

Was it helpful?

Solution

This normally happens when the Activity might have finished or in pause state before the AsyncTask is complete. onPause of the Activity set the dialog to null or dismiss it

@Override
protected void onPostExecute(Void result) {
    if(!isPaused && dialog.isShowing()){
        dialog.dismiss();  <-------- HERE IT HAPPENS
    }
}
boolean isPaused;
@Override
protected void onPause() {
    super.onPause();
    isPaused = true;
}
@Override
protected void onResume() {
    super.onResume();
    isPaused = false;
    if(dialog.isShowing() && (asyncFinishedCheck)){
        //Dismiss code goes here
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top