导航组件在设备旋转时崩溃
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 activity和presenter
下面是我的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。
在没有方向改变的情况下,它按预期工作。
3条答案
按热度按时间vktxenjb1#
在更改配置后,导航控制器backstack出现问题
在带有片段的Activity中:在更改配置之前尝试保存导航控制器状态。更改配置后,再次将其添加回导航控制器。如:
那是我的案子
ia2d9nvy2#
这是因为您没有在
LoginNameFragment
上指定操作。vh0rcniy3#
使用这些依赖版本