Hi I am new to android NavigationManager.
Consider fragments A, B and C conected in nav_graph as shown in the picture.
Basically what I am trying to achieve is to navigate from fragment A directly to C. But when clicking on back button, I want it to behave as if I were first in fragment B.
So A->C, on back buttons: C->B->A
Is there some way, how I could add fragment B to backstack without actually showing it using NavigationManager?
So it seems I should be more careful when reading documentation. I missed this site.
There is mechanism for custom back behavior. All I needed to do was to add this code to fragment C class:
//use onResume if you want to handle orientation change
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this, object :
OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val options = NavOptions.Builder()
.setPopUpTo(R.id.fragmentC, true)
.build()
navController.navigate(R.id.fragmentB, options)
}
})
}
My mindset was wrong the whole time. I was trying to add B to backstack when launching C. What I had to do instead in fragment C on back button pressed was going forward to B and delete C from backstack.