I’m using jQM/Phonegap and I’m trying to get control over Android’s back button. This is a simplified version of a function I built:
var currFunc = undefined;
function setButton () {
var func = arguments[0];
document.removeEventListener('backbutton', currFunc, false);
currFunc = func;
document.addEventListener('backbutton', currFunc, false);
}
I now call setButton() on every pagebeforeshow event to unset any handlers. On, say, Page B, I now call setButton(function() { alert(1); });.
First, this seems to work: Nothing happens on Page A, clicking the button on Page B will cause the alert. So far so good. But now, when I’m back on Page A and I click on a button that gets me to Page B again, it will already trigger the alert.
I’m confused. The listener should a) be unset anyway and b) only fire when the phone’s backbutton is pressed, not when simply navigating within the app.
Does anyone have information or ideas on this? Thanks in advance!
Still don’t know why the original code didn’t work, but using the approach as mentioned in the comments works.