13

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?

Grokify
  • 15,092
  • 6
  • 60
  • 81
Carasel
  • 2,740
  • 5
  • 32
  • 51
  • 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. – Carasel Dec 04 '13 at 11:44
  • Check this link: http://stackoverflow.com/a/2000319/1739882 – Chintan Soni Dec 04 '13 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. – Carasel Dec 04 '13 at 11:55
  • Ah, I followed the thread of SO answers and eventually got to this one: http://stackoverflow.com/questions/136937/is-there-a-way-to-catch-the-back-button-event-in-javascript – Eric L. Mar 13 '14 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. – Xuezheng Ma Jun 18 '18 at 21:46

2 Answers2

1

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.

Navjot Ahuja
  • 1,141
  • 7
  • 10
  • 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 – Ignacio Ara Jun 14 '18 at 14:43
  • I've compared ALL event attributes and all of them have the same values :( – Ignacio Ara Jun 14 '18 at 14:57
  • 1
    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. – yeshashah Jun 16 '18 at 16:04
  • 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. – Ignacio Ara Jun 18 '18 at 07:08
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.

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60