DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法

DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法

参考文章:

  • 使用自定义DialogFragment出现IllegalStateException: Can not perform this action after onSaveInstanceState异常
  • Fragment Or DialogFragment Can not perform this action after onSaveInstanceState

解决办法:

  • 在BaseDialog中重写show()和dismiss()方法。
    @Override
    public void show(FragmentManager manager, String tag) {
//        super.show(manager, tag);
        try {
            Class c = Class.forName("android.support.v4.app.DialogFragment");
            Constructor con = c.getConstructor();
            Object obj = con.newInstance();
            Field dismissed = c.getDeclaredField("mDismissed");
            dismissed.setAccessible(true);
            dismissed.set(obj, false);
            Field shownByMe = c.getDeclaredField("mShownByMe");
            shownByMe.setAccessible(true);
            shownByMe.set(obj, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
    @Override
    public void dismiss() {
//        super.dismiss();
        dismissAllowingStateLoss();
    }

你可能感兴趣的:(安卓天坑)