5

Could someone please tell me how to disable Android's back button (That is the back-button found on all Android phones).

I am using Jquery mobile with PhoneGap. I find this online in the Cordova documentation, but this does not work for me. Back button event is not even registering.

function onLoad() {
    console.log("**** INSIDE ONLOAD FUNCTION *****");
    document.addEventListener("backbutton", onBackKeyDown, false);   
}

// Handle the back button
function onBackKeyDown() {

    // Pressing the back button does not print this message.
    console.log("**************** INSIDE BACK BUTTON *************");
}
Hardik Vaghani
  • 2,163
  • 24
  • 46
user1478810
  • 51
  • 1
  • 2
  • 1
    Users expect to be able to use the BACK button to navigate their app, or possibly leave the app. If you wish to *do* something *positive* with the BACK button, that is fine. For example, you might use it to navigate within your own app, until the user gets to the beginning, at which point you allow normal BACK button behavior to happen. Otherwise, please leave the BACK button alone. – CommonsWare Sep 23 '12 at 23:48
  • 1
    @Ouadie answer is correct. The problem with your code is you did not wait for the "deviceready" event before registering your back key event listener. – Simon MacDonald Sep 24 '12 at 13:34

2 Answers2

13

I used backKeyDown and it works for me :

function onDeviceReady() {
        document.addEventListener("backbutton", backKeyDown, true);
        console.log("PhoneGap is ready");
    }

    function backKeyDown(d) {
        navigator.app.exitApp(); // To exit the app!
        e.preventDefault(); // to disable the back
    }

make sure that PhoneGap is ready!

Update: You can leave the handler empty to disable it

Ouadie
  • 13,005
  • 4
  • 52
  • 62
0
sometimes you can get blocking Back button, sometimes not.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    document.addEventListener("backbutton", function (e) {
        e.preventDefault();
    }, false );
}
Narsingh Tomar
  • 487
  • 5
  • 15