Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't popBackStack() after screen rotation. Android Navigation Components

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

like image 236
user294076 Avatar asked Jul 31 '26 13:07

user294076


1 Answers

Solved the problem by extracting all NavControllers from ViewModel and never keep NavController instance. Only use getter methods when acces needed.

like image 99
user294076 Avatar answered Aug 02 '26 06:08

user294076



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!