Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 547
SimonSays Avatar asked Aug 24 '12 21:08

SimonSays


1 Answers

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

like image 120
Enes Avatar answered Sep 20 '22 04:09

Enes