打开后导航导致崩溃被杀死activity

Navigation causes crash after opening killed activity

我的应用程序在重新启动后崩溃 activity 使用多个返回堆栈,如底部导航选项卡 samples 所示。它仅在使用多堆栈时崩溃。

堆栈跟踪:

 Caused by: java.lang.IllegalArgumentException
    at androidx.lifecycle.LifecycleRegistry.downEvent(LifecycleRegistry.java:263)
    at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:314)
    at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
    at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
    at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:118)
    at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.java:150)
    at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.java:130)
    at androidx.navigation.NavController.popBackStackInternal(NavController.java:325)
    at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:426)
    at androidx.navigation.NavController.onGraphCreated(NavController.java:636)
    at androidx.navigation.NavController.setGraph(NavController.java:586)
    at androidx.navigation.NavController.setGraph(NavController.java:551)
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:247)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2867)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:444)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1287)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1444)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1522)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1575)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3001)
    at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2938)
    at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:240)
    at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:243)
    at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:118)
    at my.base.ui.base.DaggerActivity.onCreate(DaggerActivity.kt:26)
    at my.base.ui.base.delegate.LocalizationActivity.onCreate(LocalizationActivity.kt:29)
    at my.base.ui.base.BaseActivity.onCreate(BaseActivity.kt:34)
    at my.base.ui.base.BaseFragNavActivity.onCreate(BaseFragNavActivity.kt:37)
    at my.app.ui.MainActivity.onCreate(MainActivity.kt:40)
    at android.app.Activity.performCreate(Activity.java:5937)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
    ... 10 more

抛出异常的行:

private static Event downEvent(State state) {
        switch (state) {
            case INITIALIZED:
                throw new IllegalArgumentException();

依赖关系:

const val navigationFragment = "androidx.navigation:navigation-fragment-ktx:2.3.0"
const val navigationUi = "androidx.navigation:navigation-ui-ktx:2.3.0"

设置代码:

binding.bottomNavigation.setupWithNavController(
            listOf(
                R.navigation.nav1,
                R.navigation.nav2,
                R.navigation.nav3
            ),
            supportFragmentManager,
            R.id.container,
            intent
        ).observe(this) { controller ->
            navController = controller
            controller.addOnDestinationChangedListener(this)
        }

Xml(也尝试使用 <fragment/>):

<androidx.fragment.app.FragmentContainerView
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="androidx.navigation.fragment.NavHostFragment"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

Google issue tracker(有可重现的样本)

显然问题在于导航图和目的地的 ID 相同。 https://issuetracker.google.com/issues/161825212