0

In my activity there is a async task which connects to the internet and saves data in my database. when I change the orientation while this task is running I'll get an exception:

07-27 16:01:45.956  10173-10173/de.MayerhoferSimon.Vertretungsplan E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalArgumentException: View not attached to window manager
        at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:668)
        at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:364)
        at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
        at android.app.Dialog.dismissDialog(Dialog.java:347)
        at android.app.Dialog.dismiss(Dialog.java:330)
        at de.MayerhoferSimon.Vertretungsplan.MainActivity$1.onPostExecute(MainActivity.java:129)
        at de.MayerhoferSimon.Vertretungsplan.MainActivity$1.onPostExecute(MainActivity.java:90)
        at android.os.AsyncTask.finish(AsyncTask.java:631)

so this is what my AsyncTask is look like:

        @Override
        protected void onPreExecute() {
            isRefreshing = true;
            this.dialog.setMessage("Lädt Daten...");
            this.dialog.show();
        }
        @Override
        protected void onPostExecute(String successful) {
            if (!isFinishing()) {
                if (successful.equals("succeed")) {
                    Toast toast = Toast.makeText(context, R.string.toast_refresh_succeed, Toast.LENGTH_LONG);
                    toast.show();
                }
                else if (successful.equals("failed")) {
                    Toast toast = Toast.makeText(context, R.string.toast_refresh_failed, Toast.LENGTH_LONG);
                    toast.show();
                }
                if (this.dialog.isShowing()) {
                    this.dialog.dismiss();
                }
            }
        }

What I now want is to set a delay on the orientationchange, that this will not happen again and the orientation will change only when the AsyncTask finished.

maysi
  • 5,457
  • 12
  • 34
  • 62
  • 1
    http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998. check this might help. – Raghunandan Jul 27 '13 at 14:20
  • http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html. here's one using fragments. – Raghunandan Jul 27 '13 at 14:36

1 Answers1

0

If you find yourself having to subvert the designed flow of the platform, you are probably doing something wrong. Furthermore, there are many instances when this.dialog.isShowing() would be invalid, including when the Activity is paused (user press the home key, for example), so you have to use a generic solution that would work for all scenarios.

The solution provided here should resolve your issue.

Community
  • 1
  • 1
Kai
  • 15,284
  • 6
  • 51
  • 82