I'm building my application through PhoneGap Build online. I want to change the default behavior of Back Button
$(document).ready(function(e) {
document.addEventListener("backbutton", onBackKeyDown, false);
});
function onBackKeyDown(){
alert('back');
return false;
}
Its not working I've searched for solution. But, they all showing to change the java codes in PhoneGap library, which is not in my case. I'm submitting my application in a .zip format with config.xml inside.
Is it possible with config.xml?
First of all I did wrong as pointed by @Mejo, Thanks. Here is the solution to the problem.
Step 1: Include Script to HTML don't need it physically within application zip, as included automatically by PhoneGap Build
<script src="cordova.js"></script>
or <script src="phonegap.js"></script>
any of them will work fine.
Step 2: Add this to script to get device ready call:
document.addEventListener("deviceready", onDeviceReady, false);
Step 3: Add event listener to back button and add your code to that call:
function onDeviceReady(){
document.addEventListener("backbutton", onBackKeyDown, false);
}
function onBackKeyDown(){
alert('back');
return false;
}
Still now it will not work if you don't set preference of minSDK to application by config.xml
Step 4: Add this to preference region of config.xml
<preference name="android-minSdkVersion" value="5" />
For reference: http://community.phonegap.com/nitobi/topics/how_to_handle_back_button_in_android
Its said in Cordova API documentation that
Typically, you will want to attach an event listener with document.addEventListener once you receive the PhoneGap 'deviceready' event.
So change your code like this
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
function onBackKeyDown() {
//Your backbutton code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With