HI All, In Compose Multiplatform Web (JS), when I...
# javascript
r
HI All, In Compose Multiplatform Web (JS), when I load the content in my Android Web browser i want to override the hardware back button of Android device how to do it?
FYI cross posting to multiple channels is considered spamming.
r
@Chrimaeon import kotlinx.browser.window fun main() { // Add an initial state window.history.pushState(null, "", window.location.href) // Listen for back navigation window.onpopstate = { event -> println("Back button pressed!") // Custom logic here, e.g., navigate within the app // Optionally push another state to "trap" the back button window.history.pushState(null, "", window.location.href) } } I used the above code to achieve what i requested, the answer you gave was not satisfactory for me so I thought better to ping in #C0B8L3U69 channel, but because you commented with saying that is not possible I think no one answered my question. Please respond to the questions if you are 100 percent sure what you said is correct or else please let others answer .
c
This only happens if the back button is navigating your webpage. The brower app does not necessarily do that on each back button press. So what you have is a hack that might function but does not need to work all the time.
You asked for „override the hardware back button“ and that’s definitely not possible from JavaScript.
r
You’re right that the History API approach relies on the back button triggering navigation within the webpage, and it’s not a guaranteed catch for every scenario—sometimes the browser might just close the tab or app instead. I’d call it more of a workaround than a full override of the hardware back button, since JavaScript can’t directly hook into that. That said, it actually worked for my use case! I tested it on my Android device, and after some user interaction, the popstate event fired consistently when I hit the back button. It’s not perfect, but it got the job done for me.
t
What is your browser limitations?
Which browsers and versions you need?