【问题标题】:Nested fragment: Can not perform this action after onSaveInstanceState嵌套片段:onSaveInstanceState 后无法执行此操作
【发布时间】:2019-06-06 14:52:21
【问题描述】:

我刚刚发现使用 API 24 的 Pixel 模拟器出现永久性崩溃。 我的真机和其他一些模拟器没有这个错误。

我正在做的事情如下:在一个片段中,我尝试使用以下代码添加另一个片段:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val fragmentTransaction = requireFragmentManager().beginTransaction()
    val fragment = ComposableTableReportFragment.newInstance(null)
    fragmentTransaction.add(R.id.tableContainer, fragment)
    fragmentTransaction.commit()

这会在我的应用启动时导致以下崩溃。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.pokeranalytics.android, PID: 20332
    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at androidx.fragment.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2080)
        at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2106)
        at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
        at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
        at net.pokeranalytics.android.ui.fragment.StatisticsFragment.initUI(StatisticsFragment.kt:71)
        at net.pokeranalytics.android.ui.fragment.StatisticsFragment.onViewCreated(StatisticsFragment.kt:62)

我已经尝试了 SO 上看到的各种方法,因为崩溃非常频繁,但似乎每个案例都是独一无二的,并且没有找到适合我的解决方案。感谢您的帮助!

【问题讨论】:

    标签: android android-fragments kotlin android-lifecycle


    【解决方案1】:

    尝试切换到 -

    fragmentTransaction.commitAllowingStateLoss()
    

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2014-03-05
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      相关资源
      最近更新 更多