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 *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar