android 旋转时导航组件崩溃

u4dcyp6a  于 2023-04-04  发布在  Android
关注(0)|答案(3)|浏览(115)

导航组件在设备旋转时崩溃

2019-04-15 11:50:08.589 21493-21493/me.rankov.kaboom E/AndroidRuntime: FATAL EXCEPTION: main
    Process: me.rankov.kaboom, PID: 21493
    java.lang.IllegalArgumentException: navigation destination me.rankov.kaboom:id/actionHomeToCountry is unknown to this NavController
        at androidx.navigation.NavController.navigate(NavController.java:803)
        at androidx.navigation.NavController.navigate(NavController.java:744)
        at androidx.navigation.NavController.navigate(NavController.java:730)
        at androidx.navigation.NavController.navigate(NavController.java:718)
        at me.rankov.kaboom.login.LoginActivity.navigateToRegister(LoginActivity.kt:22)
        at me.rankov.kaboom.login.LoginPresenterImpl.checkRegistration(LoginPresenterImpl.kt:49)
        at me.rankov.kaboom.login.LoginPresenterImpl.onStart(LoginPresenterImpl.kt:37)
        at me.rankov.kaboom.login.LoginActivity.onStart(LoginActivity.kt:45)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1392)
        at android.app.Activity.performStart(Activity.java:7231)

它在这里坠毁

override fun navigateToRegister(fragmentId: Int, bundle: Bundle) {
    findNavController(R.id.login_host_fragment).navigate(fragmentId, bundle)
}

我用onStart事件从presenter调用它

val nickname = loginInteractor.getNickname()
val country = loginInteractor.getCountry()
when {
    nickname.isEmpty() -> {
        val bundle = bundleOf("user" to user)
        loginView?.navigateToRegister(R.id.actionHomeToName, bundle)
    }
    country < 0 -> loginView?.navigateToRegister(R.id.actionHomeToCountry)
    else -> loginView?.navigateToMain()
}

完整代码在这里:login activitypresenter
下面是我的host fragment:

<fragment
    android:id="@+id/login_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="395dp"
    android:layout_height="322dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/login" />

这就是navigation graph
在没有方向改变的情况下,它按预期工作。

vktxenjb

vktxenjb1#

在更改配置后,导航控制器backstack出现问题
在带有片段的Activity中:在更改配置之前尝试保存导航控制器状态。更改配置后,再次将其添加回导航控制器。如:

override fun onSaveInstanceState(savedInstanceState: Bundle) {
    super.onSaveInstanceState(savedInstanceState)
    savedInstanceState.putBundle("nav_state", fragment.findNavController().saveState())
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    fragment.findNavController().restoreState(savedInstanceState.getBundle("nav_state"))
}

那是我的案子

ia2d9nvy

ia2d9nvy2#

这是因为您没有在LoginNameFragment上指定操作。

<fragment
        android:id="@+id/loginNameFragment"
        android:name="me.rankov.kaboom.login.LoginNameFragment"
        android:label="fragment_login_name"
        tools:layout="@layout/fragment_login_name">
        <action
            android:id="@+id/actionNameToCountry"
            app:destination="@+id/loginCountryFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popUpTo="@+id/login_nav"
            app:popUpToInclusive="true" />

        <action   
           android:id="@+id/actionHomeToCountry" 
           app:destination="@+id/loginCountryFragment" 
           app:enterAnim="@anim/slide_in_right" 
           app:exitAnim="@anim/slide_out_left" 
           app:popUpTo="@+id/login_nav" 
           app:popUpToInclusive="true" />

 </fragment>
vh0rcniy

vh0rcniy3#

使用这些依赖版本

implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha04"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha04"

相关问题