10

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

SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • This is the one of the most annoying problems I have encountered in Android development. The deprecated dialog APIs did not have this problem. All the workarounds are so ugly. I still could not find a clean solution to this problem. – Ertan D. Sep 25 '12 at 19:49
  • Me neither. I'm still waiting for someone showing me a clean solution :-\ – SimonSays Sep 26 '12 at 06:35

2 Answers2

5

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

Enes
  • 2,225
  • 1
  • 19
  • 16
1

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

Basically a bug in the compatibility library.

Community
  • 1
  • 1
Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124