I'm using NavController: Navigation.findNavController(activity, R.id.statistic_host_fragment). And is everything good, until i rotate my device and after that i can't navigate() and popBackStack().
In logs: FragmentNavigator: Ignoring popBackStack() call: FragmentManager has already saved its state AND FragmentNavigator: Ignoring navigate() call: FragmentManager has already saved its state
When I saw Google Sample they didn't anything about this problem, there is all working fine. So I can't understand that is the problem.
Also when I'm using MVVM, I'm saving navController in viewModel, and after onCreate() reinit controller in viewModel. It solved navigate() problem after rotation but I still can't popBackStack(). After that I trying use Navigation.setViewNavController() but it not work.
val destId = currentController.currentDestination?.id
if (destId != R.id.wikiFragment && destId != R.id.statisticsFragment && destId != R.id.trackerFragment)
currentController.navigateUp()
and in HomeActivity:
override fun onBackPressed() {
if (vm.currentController.graph.startDestination == vm.currentController.currentDestination?.id) {
super.onBackPressed()
} else {
vm.currentController.popBackStack()
}
}
and all source : https://github.com/SuperCatDev/OverHub/tree/feature/maps
Solved the problem by extracting all NavControllers from ViewModel and never keep NavController instance. Only use getter methods when acces needed.
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