Closed diorslau closed 5 years ago
code:
NavHostFragment navHostFragment = (NavHostFragment) mFragmentManager.findFragmentById(R.id.fragment);
KeepStateNavigator navigator = new KeepStateNavigator(this, navHostFragment.getChildFragmentManager(), R.id.fragment);
NavController navController = navHostFragment.getNavController();
navController.getNavigatorProvider().addNavigator(navigator);
NavigationUI.setupWithNavController(mBinding.navigation, navController);
Probably, you need to call navController.setGraph
.
Let me close. If you have any problem, please tell me.
@diorslau did you solve this?
Update
Try removing
app:navGraph="@navigation/nav_graph"
in nav_host_fragment item.
@diorslau did you solve this?
Update Try removing
app:navGraph="@navigation/nav_graph"
in nav_host_fragment item.
great, this work
Try to extend NavHostFragment by your custom fragment StatefulNavHostFragment: NavHostFragment()
and override onCreateNavController, where you can add your naviagator.
This problem is probably occurs because nav inflater is trying to inflate your graph, and at this moment he know nothing about your navigator.
If anyone still facing the problem please remove the app:navGraph="" from your host fragment and while seting up this navigator first set navigator then set graph.
Hey! Thanks for the pointers, but when I remove app:navGraph
then val navHostFragment = parentFragmentManager.findFragmentById(R.id.navHostView)!!
throws null pointer exception. Does anyone encountered this?
Hello, I wrote it according to your code, and then it ran wrong. I hope I can help me.
Caused by: java.lang.IllegalStateException: Could not find Navigator with name "keep_state_fragment". You must call NavController.addNavigator() for each navigation type.