非法状态异常:onSaveInstanceState之后无法执行此操作

时间:2019-03-17 05:35:52

标签: android android-fragments

在我的情况下,片段事务发生在onCreate()上,并且当单击底部导航栏项时(用户输入)。大多数答案都指向此blog,这表明可以在onCreate或用户输入中提交事务。我所有的应用程序要做的就是在onCreate上加载一个片段,并在用户单击底部导航栏菜单项时切换该片段。

堆栈跟踪:

java.lang.IllegalStateException
    Can not perform this action after onSaveInstanceState
    FragmentManagerImpl.java in android.support.v4.app.FragmentManagerImpl.checkStateLoss at line 2053
    FragmentManagerImpl.java in android.support.v4.app.FragmentManagerImpl.enqueueAction at line 2079
    BackStackRecord.java in android.support.v4.app.BackStackRecord.commitInternal at line 678
    BackStackRecord.java in android.support.v4.app.BackStackRecord.commit at line 632
    MainActivity.java ? in com.myapp.MainActivity.loadFragment at line 510 (application)
    MainActivity.java ? in com.myapp.MainActivity$onNavigationItemSelectedListener$1.onNavigationItemSelected at line 110 (application)
    BottomNavigationView.java in android.support.design.widget.BottomNavigationView$1.onMenuItemSelected at line 182
    MenuBuilder.java in android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected at line 822
    MenuItemImpl.java in android.support.v7.view.menu.MenuItemImpl.invoke at line 171
    MenuBuilder.java in android.support.v7.view.menu.MenuBuilder.performItemAction at line 973
    BottomNavigationMenuView.java in android.support.design.internal.BottomNavigationMenuView$1.onClick at line 95
    View.java in android.view.View.performClick at line 6897
    View.java in android.view.View$PerformClick.run at line 26100
    Handler.java in android.os.Handler.handleCallback at line 789
    Handler.java in android.os.Handler.dispatchMessage at line 98
    Looper.java in android.os.Looper.loop at line 164
    ActivityThread.java in android.app.ActivityThread.main at line 6944
    java.lang.reflect.Method in invoke
    Zygote.java in com.android.internal.os.Zygote$MethodAndArgsCaller.run at line 327
    ZygoteInit.java in com.android.internal.os.ZygoteInit.main at line 1374

我不想使用commitWithStateLoss(),也不想使用状态变量。其他选项包括:

  1. 在commit()之前检查片段管理器isStateSaved()
  2. 检查isFinishing()以检查是否在commit()之前破坏了活动

鉴于它是从onCreate或用户输入侦听器调用的,是否有理想的解决方案来避免此崩溃?

0 个答案:

没有答案