【问题标题】:add Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState添加Fragment java.lang.IllegalStateException:onSaveInstanceState 后无法执行此操作
【发布时间】:2017-10-26 14:58:39
【问题描述】:

从一个 Activity 中,我调用我的方法来添加一个新片段:

protected void addFragment(@NonNull Fragment fragment,
                               @NonNull String fragmentTag,
                               int containerViewId, int enterAnim, int exitAnim,
                               int popEnterAnim, int popExitAnim) {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(enterAnim, exitAnim, popEnterAnim, popExitAnim)
                .add(containerViewId, fragment, fragmentTag);

        transaction = transaction.addToBackStack(null);             
        transaction.commit();
    }

大多数情况都是完美的,但很少发生崩溃(由 Crashlytics 检测到):

致命异常:java.lang.IllegalStateException:无法执行此操作 onSaveInstanceState 之后的操作 在 android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2044) 在 android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2067) 在 android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:680) 在 android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:634) 在 com.compagny.ui.BaseActivity.addFragment(BaseActivity.java:1495) 在 com.compagny.ui.BaseActivity.addFragment(BaseActivity.java:1472) 在 com.compagny.ui.training.TrainingActivity.setupView(TrainingActivity.java:309)

我不知道为什么;你对我的调查人员有什么建议吗?

非常感谢!

【问题讨论】:

标签: android android-fragments fragment


【解决方案1】:

Activity 只能在 onResume 之后和 onSaveInstanceState 之前处理片段。

  • onActivityResult 发生在 onResume 之前(不确定我为此做了什么测试,可能这个语句无效)
  • 在更改活动时,它们会脱离此状态(作为异步任务的回调)
  • 如果它们被缩小到系统中(作为异步任务的回调)

【讨论】:

    猜你喜欢
    • 2012-12-20
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多