JIGAR JIGAR - 4 months ago 17
Javascript Question

Phonegap back button i android not catched

I am working on a Phonegap app. The app is ready and now I want to add a back button event while hardware back is pressed (in android).

I have gone through a number of links with no luck.

Can anybody tell me what is the exact way to catch the back button event in a Cordova app for android. My code is below.

index.html

<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);

alert("====device ready----");
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
alert("===back button initializing==");
}

// Handle the back button
function onBackKeyDown() {
alert("====BACK PRESSED====");
}
</script>


When I press the back button, nothing happens.

Can anybody tell me what mistake I have made. Please it has wasted 6 hours. :(

Answer
make sure you have installed codova dialogs plugin:-

For example:-

cordova plugin install org.apache.cordova.dialogs

and also add following line:-

document.addEventListener("backbutton", backKeyDown, true);
                navigator.app.overrideBackbutton(true);
                function backKeyDown() {//code here
                }