打开被杀死的活动后导航导致崩溃

时间:2020-07-20 21:47:41

标签: android kotlin android-fragments navigation android-navigation

重新启动终止的活动后,我的应用程序崩溃,该活动使用多个后退堆栈,如samples中底部导航标签所示。仅在使用多堆栈时崩溃。

Stacktrace:

 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(具有可重现的样本)

1 个答案:

答案 0 :(得分:3)

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