Detect use of Android back button using JavaScript
Asked Answered
T

2

14

My site has breadcrumbs which highlight which stage in a process the user has reached. The breadcrumbs rely on the browser history to tell which stage should be highlighted when the browser back button is used, but on Android devices using the hardware back button this seems to be bypassed and the highlighted breadcrumb does not change.

My site is not using PhoneGap or anything similar as it's not usually a mobile site, so is it possible to capture use of the Android back button so that I can add an event to set the breadcrumb highlight based on the history log when the button is used, just using JavaScript or jQuery?

Travail answered 4/12, 2013 at 11:15 Comment(5)
I haven't tried anything. I just want to know if it is possible to capture the device back button, and if so, how to do it. I don't know of anything to try, as I don't know if it is even accessible to javascript.Travail
Check this link: https://mcmap.net/q/83518/-override-back-button-to-act-like-home-buttonJocelynjocelyne
Thanks, but this is not an android app, it is just a website, so I am looking for a way to detect the back button without using Java.Travail
Ah, I followed the thread of SO answers and eventually got to this one: #137437Tundra
The browser will catch the android back button event and fire popstate. But we wouldn't know if all popstate events come from android back button, or there is also a back button in the broswer.Franciscafranciscan
B
3

1. Use popstate event.

https://developer.mozilla.org/en-US/docs/Web/Events/popstate

window.onpopstate = function(e) { 
   updateBreadCrumbObservable();
};

2. Use onhashchange event.

window.onhashchange = function(e) {
   updateBreadCrumbObservable();
}

You can use event argument to get more description about the event fired.

Buddybuderus answered 13/6, 2018 at 10:19 Comment(4)
Thanks @Navjot Ahuja, I've tested and both events are launched in every navigation through the page but, how can I know if the user has launched it or it has been the android back button? Kind regardsHeffernan
I've compared ALL event attributes and all of them have the same values :(Heffernan
Why do you need to have a separate behaviour for hardware and browser back button? They should technically behave the same way for the user. In that case, answer given by @NavjotAhuja will work fine.Cecillececily
Sorry perhaps my message was unclear. I meant that how can I know if the user clicks a button (every navigation in page launches both functions) or if the user has pressed return to last page / back button in Android. Thanks and sorry again.Heffernan
P
1

You can put check on inside onBackPressed method of the activity and before calling super.onBackPressed(); trigger the method to call javascript method.

for e.g:

override fun onBackPressed() {
    if (handleBackPress) {
        myWebView.loadUrl("javascript:backButtonPressed()")
    }else {
        super.onBackPressed()
    }
}

in the above example if handleBackPress boolean variable is true, then it will try to call backButtonPressed() method of javascript file in the webview.

Let me know if you need any explanation or help.

Patronymic answered 19/6, 2018 at 5:55 Comment(5)
Nice and +1 but What's fun here?Brendabrendan
the code is in kotlin language.. which means it is a functionPatronymic
in java you can write it like, "public void onBackPressed()" .. :)Patronymic
Ohh. I don't have any idea about kotlin. Thanks.Brendabrendan
@RahulKhurana we're not using Java/Android, this is just a mobile site that uses JavaScriptHeffernan

© 2022 - 2024 — McMap. All rights reserved.