JIGAR JIGAR - 1 year ago 168
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.


<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====");

When I press the back button, nothing happens.

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

Answer Source
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);
                function backKeyDown() {//code here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download