Overriding Android back button behavior in Apache Cordova

January 24, 2013 in Ajax, JavaScript, mobile, Web 2.0

If you are using Apache Cordova for building a native application on mobiles using HTML, CSS, and JavaScript. You may need to override the default behavior of the back button on the Android mobiles to implement your application specific back button behavior.

In order to do this, you can register an event handler on the "backbutton" event on the document object but before you do this, you need to make sure that the device is ready using the "deviceready" event as follows.

// In the initialization ...
document.addEventListener("deviceready", deviceReadyHandler, false);  

function deviceReadyHandler () {
    document.addEventListener("backbutton", backButtonHandler, false);

function backButtonHandler(e) {
    //Here implement the back button handler

2 responses to Overriding Android back button behavior in Apache Cordova

  1. This is not working in COrdova 3.7.1. The back button is not detected.

Leave a reply

Your email address will not be published. Required fields are marked *

Skip to toolbar