【问题标题】:what could be the reason even if check isFinishing() it still gets IllegalStateException: Can not perform this action after onSaveInstanceState即使检查 isFinishing() 仍然得到 IllegalStateException: Can not perform this action after onSaveInstanceState 可能是什么原因
【发布时间】:2017-05-11 21:37:59
【问题描述】:

getting IllegalStateException: 添加片段时,在活动中 onSaveInstanceState 后无法执行此操作。它大部分时间都有效,但有时会发生这种 IllegalStateException。

在检查活动是否仍然存在后完成添加片段:

if ((!mInSaveInstanceState) && !isFinishing())

任何解释为什么“IllegalStateException:onSaveInstanceState 后无法执行此操作”仍然可能发生?

代码 sn-p 很糟糕。

活动中:

@Override
protected void onSaveInstanceState(Bundle outState) {
    mInSaveInstanceState = true;
    … …
    super.onSaveInstanceState(outState);
}

sn-p 显示一个新片段:

void addNewFragment () {
    if ((!mInSaveInstanceState) && !isFinishing()) {
        FragmentManager fm = getSupportFragmentManager();
        String fragmentName = "NEW_ADDED_FRAGMENT";
        int holderId = R.id.new_fragment_holder;
        NewFragment theNewFrgmt = new NewFragment();

        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(holderId, theNewFrgmt, fragmentName);
        ft.addToBackStack(fragmentName);


        ft.setCustomAnimations(R.anim.hold, 0, R.anim.slide_left_in, 0);
        ft.show(theNewFrgmt);
        ft.commit();  //<=== it throws at here
    }
}

崩溃

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1832)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1850)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:643)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:603)
    at com.zone.ui.MainActivity.addNewFragment(MainActivity.java:4522)
    at com.zone.ui.MainActivity.access$000(MainActivity.java:262)
    at com.zone.ui.MainActivity$8.onEvent(MainActivity.java:591)
    at com.zone.ui.MainActivity$8.onEvent(MainActivity.java:587)
    at com.zone.dataModel.EventManager.dispatchEvent(EventManager.java:96)
    at com.zone.ui.tasklist.TaskListFragment$8.run(TaskListFragment.java:422)
    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:5343)
    at java.lang.reflect.Method.invoke(Method.java)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

【问题讨论】:

    标签: android android-fragments onsaveinstancestate


    【解决方案1】:

    onSaveInstanceState 之后不能提交事务,所以使用 StateLoss 提交。

    ft.commitAllowingStateLoss();
    

    【讨论】:

    • 感谢维杰!想知道为什么在使用 commitAllowingStateLoss 之前仍然会发生这种情况(据报道有一些意想不到的效果)。
    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2012-07-25
    • 1970-01-01
    • 2012-08-15
    • 2019-05-03
    • 2022-02-09
    • 2016-12-24
    • 2011-11-06
    相关资源
    最近更新 更多