Search code examples
javaandroidandroid-fragmentsandroid-dialogfragment

Dismissing a dialog fragment which is launched from another dialog fragment results in illegal state exception


DialogFragment customCurrentDialogFragment; -> custom dialog

Launching the dialog from another dialog fragment(fragment A) :

FragmentManager fragmentManager = getFragmentManager();  
customCurrentDialogFragment = new CustomLoader();  
customCurrentDialogFragment.setCancelable(false);  
customCurrentDialogFragment.show(fragmentManager, "dialog");

Dismissing in fragment A :

customCurrentDialogFragment.dismiss();

Solution

  • You can use dismissAllowingStateLoss() method of DialogFragment.

    Since you are calling show() method using fragment manager, you could use dismissAllowingStateLoss() method to dismiss the dialog fragment. Meanwhile this doesn't work if you use show() method using fragment transaction because popBackStack calls enqueueAction() with allowStateLoss = false