-
-
Save macdonst/955496 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
"http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<title>PhoneGap Back Button Example</title> | |
<script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script> | |
<script type="text/javascript" charset="utf-8"> | |
// Call onDeviceReady when PhoneGap is loaded. | |
// | |
// At this point, the document has loaded but phonegap.js has not. | |
// When PhoneGap is loaded and talking with the native device, | |
// it will call the event `deviceready`. | |
// | |
function onLoad() { | |
console.log("I've been loaded"); | |
document.addEventListener("deviceready", onDeviceReady, false); | |
} | |
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods | |
// | |
function onDeviceReady() { | |
console.log("Device Ready"); | |
} | |
/** | |
* Global Variables | |
*/ | |
var cur = null; | |
// Back key event handler | |
// | |
function onBackKey() { | |
console.log("I've caught a back key"); | |
// We are going back to home so remove the event listener | |
// so the default back key behaviour will take over | |
document.removeEventListener("backbutton", onBackKey, false); | |
// Hide the current dive and show home | |
document.getElementById(cur).style.display = 'none'; | |
document.getElementById('home').style.display = 'block'; | |
cur = 'home'; | |
} | |
function goToDiv(id) { | |
// We are moving to a new div so over ride the back button | |
// so when a users presses back it will show the home div | |
document.addEventListener("backbutton", onBackKey, false); | |
// Hide home and show the new div | |
document.getElementById('home').style.display = 'none'; | |
document.getElementById(id).style.display = 'block'; | |
cur = id; | |
} | |
</script> | |
</head> | |
<body onload="onLoad()"> | |
<div id="home"> | |
Back Button Home<br/> | |
<a href="javascript:goToDiv('div1')">Div One</a><br/> | |
<a href="javascript:goToDiv('div2')">Div Two</a> | |
</div> | |
<div id="div1" style="display: none"> | |
Div One | |
</div> | |
<div id="div2" style="display: none"> | |
Div Two | |
</div> | |
</body> | |
</html> |
macdonst
commented
Dec 16, 2011
via email
Hi Simon.
I would like to provide feedback and ask a question.
Because the back button event using document.addEventListener('backbutton', ...) triggers on a button-down action, holding the back button down can cause a cascade of back button actions. e.g. from page 2 to page 1 to exiting the app.
I think "back button up" (NOT back button down) should trigger the PhoneGap back button event.
Is the success callback (e.g. onBackKey) needed for the document.removeEventListener("backbutton", onBackKey, false) API call? Logically, why would onBackKey be called when I am disabling backbutton handling?
Thanks,
David
Hey David, I saw your message on my blog as well. I agree with you and I've opened a bug to move the backbutton event from keyDown to keyUp.
Thanks Simon. As usual, your attention is greatly appreciated.Do you have a comment on the success callback for the remove backbutton event? David
HI Simon. I meant as to how to call the API - should I be providing the same OnSuccess callback in the removeEventListener? And if I do, will that callback be invoked when I do the removeEventListener? I'm trying to clarify the API spec so that I do the right things in OnSuccess. Thanks, David.
Yes, you need to provide the same function name. If you don't provide the same function name the removeEventListner won't know what call back to remove. Also, the callback is not executed during the remove function.
Great. Thanks for your answer and reference link.
Hi macdonst, thanks for your example.But I got a problem here, when run in emulator I can hide an element after push on Backbutton, but when I try on real device, the element wasn't hide after touching on screen.
My device is Lenovo P700i, android 4.0. Do you know what was the reason of this problem?