2

We're using Dialogfragment to show some dialogs and sometimes these cause an "IllegalStateException: Can not perform this action after onSaveInstanceState".

In the past this happened frequently and I was able to reduce the number of this IllegalStateException by writing a DialogHelper to show/dismiss dialogs.

Unfortunately, sometimes I still get crash reports about this and had another look at it and I'm just not able to find out how to finally fix this problem correctly.

The reported crashes at the moment are happening onClick of a button and we want to show a dialog (for example DateSliderDialog) but I can't reproduce the Crash :/.

mFromDateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isFinishing()) {
                String title= getString(R.string.dateSliderTitle);
                DateSliderDialog dateSliderDialog= DateSliderDialog.newInstance(title, getMinFromDate().getTimeInMillis(), getFromDateForDialog().getTimeInMillis(), DateUtils.getEndOfUniverseDate().getTime());
                DialogHelper.showDialog(getSupportFragmentManager(), dateSliderDialog, InquireReservationActivity.this, String.valueOf(FROM_DATE_DIALOG_ID));
            }
        }
    });

which calls DialogHelper.showDialog(...) method

    public static void showDialog(FragmentManager fragmentManager, DialogFragment dialogFragment, Context context, String dialogTag) {
    if (fragmentManager != null && dialogFragment != null && (dialogFragment.getDialog() == null || !dialogFragment.getDialog().isShowing())) {
        if (context instanceof MyActivity) {
            if(!((MyActivity)context).isFinishing() && ((MyActivity)context).mIsActivityRunning) {
                dialogFragment.show(fragmentManager, dialogTag);
            }
        } else if (context instanceof MyListActivity) {
            if(!((MyListActivity)context).isFinishing() && ((MyListActivity)context).mIsActivityRunning) {
                dialogFragment.show(fragmentManager, dialogTag);
            }
        } else if (context instanceof MyPreferenceActivity) {
            if(!((MyPreferenceActivity)context).isFinishing() && ((MyPreferenceActivity)context).mIsActivityRunning) {
                dialogFragment.show(fragmentManager, dialogTag);
            }
        } else {
            if(!((Activity)context).isFinishing()) {
                dialogFragment.show(fragmentManager, dialogTag);
            }
        }
    }
}

As you can see, I'm already checking for:

  • Is Activity finishing?
  • Is Activity running (through a flag on the Activity)?

Does anyone have a clue why this is still happening and how to fix it?

Thanks for your suggestions and answers

byemute
  • 643
  • 7
  • 15

1 Answers1

3

Whenever you are dismissing the dialog, use

  dialogFragment.dismissAllowingStateLoss();
Nidhi
  • 777
  • 7
  • 17
  • I already do this and that fixed all the issues when dismissing but not when showing a dialogFragment. The above described problem happens just for showing dialogFragments, not for dismissing. – byemute Aug 24 '16 at 11:08
  • please refer to the following thread : http://stackoverflow.com/questions/10114324/show-dialogfragment-from-onactivityresult – Nidhi Aug 24 '16 at 11:24
  • I already checked that one before posting my question. The only solution I see there is to use a FragmentTransaction with commitAllowingStateLoss() as my DialogFragment is shown onClick and I cannot control if the onClick happens before or after the state was saved. Are there any other solutions than using the dialogFragment.show() with a FragmentTranscation where I set commitAllowingStateLoss()? – byemute Aug 24 '16 at 11:27
  • I ended up using the show() method with FragmentTranscation where I can set commitAllowingStateLoss(). I'll see if that helps in the next release. – byemute Sep 02 '16 at 08:54
  • I'm curious to know if that solution worked for you. – Abdul Mateen May 15 '20 at 07:15