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:
function confirmCallback(buttonIndex) {
if(buttonIndex == 1) {
navigator.app.exitApp();
return true;
}
else {
return false;
}
}
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>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" charset="UTF-8"/>
<link rel="stylesheet" href="themes/theme.min.css" />
<link rel="stylesheet" href="css/jquery.mobile.structure-1.3.2.min.css" />
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery.mobile-1.3.2.min.js"></script>
<script src="cordova.js">
<script>
function onLoad() {
document.addEventListener('deviceready', deviceReady, false);
}
function deviceReady() {
document.addEventListener('backbutton', backButtonCallback, false);
}
function backButtonCallback() {
navigator.notification.confirm('do you want to exit the app?',confirmCallback);
}
function confirmCallback(buttonIndex) {
if(buttonIndex == 1) {
navigator.app.exitApp();
return true;
}
else {
return false;
}
}
</script>
</head>
<body onload="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"