Android Navigation popBackStack

alezhka picture alezhka · Jan 25, 2019 · Viewed 9.4k times · Source

I tried use Android Navigation component and have a problem with back stack.

I have Fragment A, B. I write:

Navigation.findNavController(view).navigate(R.id.a_to_b)

It's ok. But if I want return to A, i call:

Navigation.findNavController(view).popBackStack();

Then in fragment B will be calling onViewStateRestored with null state why?

I want to keep state when calling navigate to Fragment A.

Answer

Deepak Rajput picture Deepak Rajput · Mar 26, 2019

try this

val controller = Navigation.findNavController(view)
controller.popBackStack(R.id.id_of_fragment_which_you_want_to_skip, true)