一、问题
DialogFragment 偶现IllegalStateException: Can not perform this action after onSaveInstanceState。一般的解决方法,使用commitAllowingStateLoss 替代 commit,但DialogFragment的show方法默认使用的commit,无法修改
二、解决方案
public class AllowStateLossDialogFragment extends DialogFragment {
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
try {
FragmentManager temp = null;
// super 里有变量需要赋值
super.show(temp, tag);
} catch (Exception e) {
}
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}
}
重写父类show方法,传入空的FragmentManager,目的是触发父类show方法里需要赋值的变量被赋值,然后在try-catch之后手动调用commitAllowingStateLoss
三、结语
- 送人玫瑰 手留余香
- 如果对您有帮忙,请点赞支持我~~