For anyone using `bindToNavigation`, what's the ri...
# compose-web
w
For anyone using
bindToNavigation
, what's the right way to navigate back in common code? Neither
popBackStack
nor
navigateUp
replicate browser back for me. Normal navigation does something like: A -> B -> C -> browser back -> B -> browser back -> A Whereas the nav methods go: A -> B -> C -> popBackState/navigateUp -> B -> browser back -> C -> browser back -> B
k
because the browser back is NOT the app back stack. it is just a history of your browser
and browsers have a unique feature over other platforms: the forward button mind blown
(I guess the idea of the forward button will help you to understand how it works)
w
Hmm... Then how does one implement a back button? Is there a way to invoke browser back from common code? I don't understand why the existence of forward changes the behavior, since forward is just a replay of the navigation event that took the user to C. Since navigation can build the nav graph from just the string routes, there's no reason any other platform couldn't also record and replay the nav events.
k
Apps using the
androidx.navigation
class integrate with the browser History API to provide a consistent experience when using the browser's back and forward buttons. Whenever you navigate using the
navController
, a History API entry is added to the browser's history stack. Pressing the back button uses reverse chronological navigation, meaning that the user is taken to the previously visited location that was shown using the
navController
. This means that if the user pops a page from the
back stack
and then presses the browser back button the previous page is pushed back onto the stack.
w
I mean, I don't actually care what the navigation model is, I just need the arrow back button on the screen to match what the system back does, regardless of what that behavior is. I guess the answer is that it's not possible and I just need to wrap
NavHostController
to delegate it to
window.history.popState(
)? When I'm using an app, which one I hit is basically determined on a whim, and the fact the 2 don't have the same behavior is weird.
k
what the system back does
there is no a such thing as "system back"
only android has a system back event
w
System back just describes the user gesture. On Android that's back button/gesture. On browser that's the back button. On desktop that's mouse 4/escape. On iOS that's either back gesture or nonexistent. I just need to make all platforms consistent.
k
Browser back button has own behaviour patter and CMP supports it. if you need to customize it, you are able to do it for your case. just take a look at the CMP implementation. there are no internals usages
a
you could remove the back button altogether, and only let the user use the browser back
w
Yeah, but I think I want the user to have the ability to ignore the system specific navigation and use just the app. I ended up with the wrapping approach, calling
window.history.back()
on wasmJs and
popBackStack
on other platforms.