SimonSays
SimonSays

Reputation: 10977

IllegalStateException when using DialogFragment

I just started to use DialogFragment from the android support library and find it extremely annoying so far. I have some custom AsyncTasks that are called from different places in my app. There are two occasions when I run into problems with the DialogFragments:

  1. When debugging and the screen turns off
  2. When I want to open a FragmentDialog from onActivityResult()

Both, at least I think, are fairly common situations, and in both cases I get a

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

This is how my AsyncTasks are structured:

private class UploadImageAsyncTask extends AsyncTask<Void, Void, Image> {
    private ProgressDialogFragment dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        FragmentManager fm = getSupportFragmentManager();
        dialog = new ProgressDialogFragment();
        dialog.show(fm, "ProgressDialogFragment");
    }

    @Override
    protected Image doInBackground(Void... params) {
        ...
    }

    @Override
    protected void onPostExecute(Image result) {
        super.onPostExecute(result);
        dialog.dismiss();

        ...
        }
    }
}

I know i could set a setting that prevents the screen from going to sleep while debugging and i could set a flag in onActivityResult() and then open the dialog in onStart(), but that is not really what I'm looking for. Are there any better solutions??

Thanks Simon

Upvotes: 10

Views: 10302

Answers (2)

Enes
Enes

Reputation: 2225

use dialog.dismissAllowingStateLoss(); instead of dialog.dismiss();

Upvotes: 5

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28561

You've got an anwser on this other question: Show DialogFragment from onActivityResult

Basically a bug in the compatibility library.

Upvotes: 1

Related Questions