1

In my app I use DialogFragments.

Some times, i am getting

Fragment already added IllegalStateException

and the app crashes. These crashes seen on crashlytics.

This problem seems to happen when trying to show the same dialog fragment twice before the first instance dismissed.

This issue already discussed many times like here or here but the problem is, that it's not constant behavior on all devices.

I couldn't reproduce this problem on my desk. Even when I try to show the same dialog twice, I am not getting this exception.

I even used fragment.isAdded() to make sure that it is shown, and then show it again. Just to catch the error of course. but no success.

Any Idea? I can add fragment.isAdded() condition before showing the dialog to prevent this, but I want to understand the root cause of this. I am concerned that I cannot reproduce this.

Here is a portion of my code to show the dialog: MyDialog extends DialogFragment

 MyDialog dialog = (MyDialog) getSupportFragmentManager().findFragmentByTag(MyDialog.TAG);

    if(dialog == null) { //create and show the dialog
        dialog = new MyDialog();
        dialog.setCancelable(false);
    }
        dialog.show(getSupportFragmentManager(), MyDialog.TAG);
Moti Bartov
  • 2,642
  • 22
  • 31

1 Answers1

0

I think I've found the problem.

First, using dialog.isAdded() helped here.. But the issue here was related to instanceState of the app.

As I said, this error not happen on all devices or users, so it was hard to catch.

First, My app is configured to be portrait only. When the dialog opens, if the user navigates to some other app from some reason (e.g a phone call or other app) which in turn causes the my app to go into onStop and also that other app causing configuration change.

For instance, when the dialog opens, press the home button and go to phone settings and change the language, this will cause configuration change a the app will be recreated when navigating back to it.

When the user navigate back to my app, it killed by the system and it tries to recreate the stack and onSaveInstanceState is not null, the dialog is already in the stack and my app tries to show it again.

This kills the app.

Moti Bartov
  • 2,642
  • 22
  • 31