On showing dialog I get “Can not perform this action after onSaveInstanceState”

This is common issue.
We solved this issue by overriding show() and handling exception in DialogFragment extended class

public class CustomDialogFragment extends DialogFragment {

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commit();
        } catch (IllegalStateException e) {
            Log.d("ABSDIALOGFRAG", "Exception", e);
        }
    }
}

Note that applying this method will not alter the internal fields of the DialogFragment.class:

boolean mDismissed;
boolean mShownByMe;

This may lead to unexpected results in some cases. Better use commitAllowingStateLoss() instead of commit()

Leave a Comment

tech