Jquery Mobile And Android Device Back Button With Build.phonegap.com
I'm developing an android mobile application using jquery mobile and Phonegap. I'm new to developing android apps using Phonegap. I need to control the function of a back button in
Solution 1:
You will want to add an event listener for the back button:
document.addEventListener('backbutton', backButtonCallback, false);
Then create a function to run whatever you want when it's clicked:
function backButtonCallback() {
navigator.notification.confirm('do you want to exit the app?',confirmCallback);
}
And then a callback to close the app if the user wants to:
functionconfirmCallback(buttonIndex) {
if(buttonIndex == 1) {
navigator.app.exitApp();
returntrue;
}
else {
returnfalse;
}
}
Additionally for PhoneGap Build you will want to add this to your config.xml file:
<gap:plugin name="org.apache.cordova.dialogs" />
This will allow for the use of the confirm notification.
UPDATE:
Here is a light mod to your html:
<!DOCTYPE html><html><head><metacharset="utf-8"><metaname="viewport"content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height"charset="UTF-8"/><linkrel="stylesheet"href="themes/theme.min.css" /><linkrel="stylesheet"href="css/jquery.mobile.structure-1.3.2.min.css" /><scriptsrc="js/jquery-1.10.2.min.js"></script><scriptsrc="js/jquery.mobile-1.3.2.min.js"></script><scriptsrc="cordova.js"><script>functiononLoad() {
document.addEventListener('deviceready', deviceReady, false);
}
functiondeviceReady() {
document.addEventListener('backbutton', backButtonCallback, false);
}
functionbackButtonCallback() {
navigator.notification.confirm('do you want to exit the app?',confirmCallback);
}
functionconfirmCallback(buttonIndex) {
if(buttonIndex == 1) {
navigator.app.exitApp();
returntrue;
}
else {
returnfalse;
}
}
</script></head><bodyonload="onLoad()">
You need to make sure you include the cordova.js
always, and then using the event listener for device ready will ensure cordova is loaded before you do anything with the API. This should work now.
Post a Comment for "Jquery Mobile And Android Device Back Button With Build.phonegap.com"