navigation component question: I have fragments A,...
# android
s
navigation component question: I have fragments A,B,C I want to have a shortcut of going from A to C, but also have a way to popup from C to B what is the recommended way to do this?
r
s
do you mean global action C->B?
r
no, with a global action you can navigate to any destination from any fragment
s
I’m not sure how this helps me
r
you want to go from A to C, but also from C to B?
s
I want to go A to C (skipping B) while keeping B in the backstack so I can go back to B when backpressed
I tried action C -> B with popup but it doesn’t seem to do anything
r
ok so when you're on fragment A, just call if(conditional nav is true) findnavcontroller.navigate(to B) and also findnavcontroller.navigate(to C). You will end up on C with B on the back stack
override the animation toB
so it's not visible to the user
I think there is also an manuel way off adding fragments to the backstack with a navcontroller
check the docs, never needed it
s
thanks for help, I’ll try
r
If this does not work well UI wise, you could also override onBackPressed() in your activity, and place your logic there
s
I do, but my action doesn’t work, probably because there is no fragment B on the backstack
r
Copy code
override fun onBackPressed() {
    val navController = findNavController(R.id.nav_host_fragment)
    if (navController.currentDestination?.id == fragment C) {
that's when a global destination comes in hand
s
I use
Copy code
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) { navigator.goBack() }
in the fragment
r
check with debugger the current backstack
s
Copy code
I/NavController: Ignoring popBackStack to destination <http://com.xxx.xxx:id/b_fragment|com.xxx.xxx:id/b_fragment> as it was not found on the current back stack
r
instead of calling .popBackStack when on fragment C
just call navigate to B
inside onBackPress in your activity
or your fragment with the dispatcher you showed
s
this feels hacky, I think I could use a deep link? no real samples of this anywhere
but I see I can set a destination inside a deep link
r
why hacky? you go from A to C, but then want to magically go to B when pressing back
you need to override the default behavior in that case, because going back would mean go to A
so you override onbackpressed in your hosting activity
I usually tie a viewmodel to the activity lifecycle which I use to communicate from the fragment to the activity
so when you're on C, the user performs the action to go to B, set a boolean value in the viewmodel to true, and read this boolean inside onbackPressed in the activity
if it's true, than apply the going to B route, if it's false then just call super.Onbackpressed
easiest option, can also be done with SafeArgs
but that involves bundles and keys
s
overriding onbackpressed this way and making a cascade if-elses will cripple maintainability
ideally I would like to navigate and set backstack fragments myself
r
try the first option I gave you
navigating twice
if that doesnt work UI wise, this is an alternative
you decide what's best for your codebase
goodluck!
s
I have a long registration flow, so I may end up doing 10 navigates A-B-C-D-E-F-G-H-I-J
I feel this will break fast
r
option 2 then 😉
s
thanks for discussion
👍🏻 1