1

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.

Community
  • 1
  • 1
marty
  • 651
  • 7
  • 13

2 Answers2

1

You should add this in the manifest.xml

upto API level 12

   <activity
        android:label="@string/app_name"
        android:name=".Activity_calling_AsyncTask"
        android:configChanges="keyboardHidden|orientation">

after apl level 12,

   <activity
        android:label="@string/app_name"
        android:name=".Activity_calling_AsyncTask"
        android:configChanges="keyboardHidden|orientation|screensize">
  </activity>

hope this will help you.

Rajendra
  • 1,700
  • 14
  • 17
  • Is that supposed to be AsyncTask instead of AsyncTast? – Chad Schultz Sep 21 '12 at 16:33
  • I think where @Rejendra is going with this is a situation I've run into: even when your app is forced to portrait mode, SOME devices may start in landscape for a fraction of a second, shut down the Activity and restart in portrait mode, causing problems with dialog tasks and possibly running background tasks twice. The code Rajendra provides should take care of that; if not, check and see if your Activity is starting multiple times and code around it. – Chad Schultz Sep 21 '12 at 16:38
  • As well as orientation changes, the above config also prevents a keyboard change from causing your Activity to be restarted. This can occur when a user slides out a hardware keyboard for example. However the above solution is more of a band-aid over the issue, rather than fixing the root cause. See [this answer](http://stackoverflow.com/a/7990543/512958) for details. – dave.c Sep 21 '12 at 17:02
0

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:

  1. Implementing the AsyncTask as part of a class which inherits from Application class, which is explained here: Handle screen orientation changes when there are AsyncTasks running
  2. Implement onRetainNonConfigurationInstance() / setRetainInstance(). An example of the first is given here: http://twigstechtips.blogspot.com/2011/11/for-my-app-moustachify-everything-i-was.html - however, the method itself is deprecated, so it would be better to use setRetainInstance in a similar fashion.

And just for completeness, there's also the Droid-Fu for Android library, which claims to be able to solve this problem.

Community
  • 1
  • 1
marty
  • 651
  • 7
  • 13