Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return to the root of a navigation graph?

How can I return to the root Fragment using the NavController? I tried

navController.popBackStack(navController.graph.startDestination, false)

but it did not work. This did work:

navController.navigate(navController.graph.startDestination, false)
navController.popBackStack(0, false)

but I'm wondering if this is a proper way to solve this problem.

like image 893
artkoenig Avatar asked Jan 22 '19 15:01

artkoenig


People also ask

How can we navigate to previous fragment?

Use Up or Back button to go to a previous step of the order flow.

How do you complete a fragment navigation component?

How do I get rid of current fragment navigation component? You add to the back state from the FragmentTransaction and remove from the backstack using FragmentManager pop methods: FragmentManager manager = getActivity(). getSupportFragmentManager();


1 Answers

you can use this method, but your arguments are wrong, The first argument is the ID of destination, for back to root you have to use:

findNavController().popBackStack(R.id.Dashboard, false)

Where Dash is the ID of view on your navigation graph

like image 154
Lucas Paim Avatar answered Oct 27 '22 13:10

Lucas Paim