调用DialogFragment出现的Can not perform this action after onSaveInstanceState

阅读更多
代码里面用如下的业务场景:进页面并判断是否签到,没有签到就跳转至一个DialogFragment

最开始的做法是如下:
SignInFragment signInFragment = new SignInFragment();

signInFragment.setTargetFragment(LauncherFragment.this,ConstantNewValue.FRAGMENT_RESPONSE_CODE_SIGN);

signInFragment.show(getActivity().getSupportFragmentManager(), "signInFragment");


结果报错
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
    at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
    at com.gty.ryt.admin.fragment.LauncherFragment$2.onNext(LauncherFragment.java:112)
    at com.gty.ryt.admin.subscribers.ProgressSubscriber.onNext(ProgressSubscriber.java:75)
    at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:134)
    at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:224)
    at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5288)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:710)


检查发现show()方法调用的还是fragment.commit()的方法
    public void show(FragmentManager manager, String tag) {
        mDismissed = false;
        mShownByMe = true;
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commit();
    }


所以针对DialogFragment修改如下
SignInFragment signInFragment = new SignInFragment();                                  

signInFragment.setTargetFragment(LauncherFragment.this,ConstantNewValue.FRAGMENT_RESPONSE_CODE_SIGN);                        

holder.fragmentManager.beginTransaction().add(signInFragment,"signInFragment").commitAllowingStateLoss();

你可能感兴趣的:(android,fragment,DialogFragment)