My Problem is similar to this one:
Error on dismissing ProgressDialog in AsyncTask
I have an AsyncTask that creates a dialog in onPreExecute like this:
dialog = ProgressDialog.show(activity, "login", "logging in, one moment please");
And dismisses the dialog in onPostExecute like this:
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Still, i get error reports from users on the line where i do diolog.dismiss() with the message:
java.lang.IllegalArgumentException: View not attached to window manager
The most common root-cause is – or so i’ve read – when a user switches orientation (from portrait to landscape or vice versa).
My app however is forced to portrait mode, so this can not be the cause. (i’ve double checked this to make sure it really is not possible)
The reporter of the before mentioned post solved it in the end (and i’ve read this solution elsewhere too) by creating an inner class for the AsyncTask in the activity class and working with onCreateDialog from the activity class and calling showDialog from the AsyncTask. (read his post if you don’t understand this)
I’ve begun trying to implement that, but it seems that showDialog is deprected: so this is not a solution for me.
The last solution i’ve found is by simply catching the Exception. I had thought of that myself too, but only as a last resort. I prefer to understand what really goes wrong and anticipate on that instead of simply catching the exception and not knowing what is going on.
If what Chad is saying is correct – that some devices might start in landscape for a fraction of a second – then i think this is the most likely cause for getting the error-reports. While the solution of Rajendra might be able to prevent that, i’ve decided it really does feel like applying a band-aid as others have pointed out, since screen-rotation is not the only source of the real problem.
After some more searching, i’ve found two solutions:
And just for completeness, there’s also the Droid-Fu for Android library, which claims to be able to solve this problem.