Navigation component crash on the device rotate
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)
It crash here
override fun navigateToRegister(fragmentId: Int, bundle: Bundle) {
findNavController(R.id.login_host_fragment).navigate(fragmentId, bundle)
}
I call it from presenter with onStart event
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()
}
The full code is here: login activity and presenter
Here is my 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" />
Here is the navigation graph.
Without orientation change it works as expected.
There is a problem with nav controller backstack after changed config
In your Activity with fragment: try saving the nav controller state before changing the configuration. After changing the configuration, add it back to the nav controller again. Like:
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"))
}
That was my case
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With