Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can you handle dismissing a DialogFragment (compatibility lib) upon completion of an AsyncTask
    text
    copied!<p>There are numerous posts about how to handle a configuration change during an AsyncTask, but none I have found give a clear solution regarding apps that are in background (onPause()) when an AsyncTask finishes and tries to dismiss a DialogFragment (compatibility library). </p> <p>Here is the problem, if I have an AsyncTask running that should dismiss a DialogFragment in onPostExecute(), I get an IllegalStateException if the app is in the background when it tries to dismiss the DialogFragment.</p> <pre><code>private static class SomeTask extends AsyncTask&lt;Void, Void, Boolean&gt; { public SomeTask(SomeActivity tActivity) { mActivity = tActivity; } private SomeActivity mActivity; /** Set the view during/after config change */ public void setView(Activity tActivity) { mActivity tActivity; } @Override protected Boolean doInBackground(Void... tParams) { try { //simulate some time consuming process TimeUnit.SECONDS.sleep(3); } catch (InterruptedException ignore) {} return true; } @Override protected void onPostExecute(Boolean tRouteFound) { mActivity.dismissSomeDialog(); } } </code></pre> <p>The Activity looks like this:</p> <pre><code>import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; public class SomeActivity extends FragmentActivity { public void someMethod() { ... displaySomeDialog(); new SomeTask(this).execute(); ... } public void displaySomeDialog() { DialogFragment someDialog = new SomeDialogFragment(); someDialog.show(getFragmentManager(), "dialog"); } public void dismissSomeDialog() { SomeDialogFragment someDialog = (SomeDialogFragment) getFragmentManager().findFragmentByTag("dialog"); someDialog.dismiss(); } .... } </code></pre> <p>Works fine UNLESS the app switches to background while SomeTask is still running. In that case, when SomeTask tries to dismissSomeDialog(), I get an IllegalStateException.</p> <pre><code>05-25 16:36:02.237: E/AndroidRuntime(965): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState </code></pre> <p>All of the posts I've seen seem to point in some kludgy direction with elaborate workarounds. Isn't there some android way of handling this? If it were a Dialog instead of a DialogFragment, then the Activity's dismissDialog() would handle it correctly. If it were a real DialogFragment instead of one from the ACP, then dismissAllowingStateLoss() would handle it. Isn't there something like this for the ACP version of DialogFragment?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload