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?

5
  • 4
    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. Commented Dec 4, 2013 at 11:44
  • Check this link: stackoverflow.com/a/2000319/1739882 Commented Dec 4, 2013 at 11:53
  • 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. Commented Dec 4, 2013 at 11:55
  • Ah, I followed the thread of SO answers and eventually got to this one: stackoverflow.com/questions/136937/… Commented Mar 13, 2014 at 16:53
  • 1
    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. Commented Jun 18, 2018 at 21:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 regards
I've compared ALL event attributes and all of them have the same values :(
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.
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.
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.

4 Comments

Nice and +1 but What's fun here?
the code is in kotlin language.. which means it is a function
in java you can write it like, "public void onBackPressed()" .. :)
@RahulKhurana we're not using Java/Android, this is just a mobile site that uses JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.