Tip #3: Implementing the Back button behavior for Android and Windows Phone 8 Apps

If you use Apache Cordova for developing your Android and Windows Phone 8 mobile apps, you may need to exit the app when the user clicks on the back button on the app’s home page, and to navigate back when the user clicks the back button and the current page is not the home page.

Note that back button usually exists in Android and Windows Phone 8 devices.

In order to do implement this behavior, you can implement the “backbutton” event handler after the “deviceready” event is triggered. This is an example to implement this in jQuery mobile.

    var homePage = "appHome";
    //Handle back buttons decently for Android and Windows Phone 8 ...
    function onDeviceReady() {
        document.addEventListener("backbutton", function(e){
            if ($.mobile.activePage.is('#' + homePage)){
            } else {
        }, false);

    $(document).ready(function() {
        document.addEventListener("deviceready", onDeviceReady, false); 

Using navigator.app.exitApp(), you can exit your mobile app when the app user is in the home page. And using history.back(), you can navigate back to the previous page when the app user is in other pages.


“JavaScript Mobile Application Development” Book:

Changing the moveTo attribute in “dojox/mobile/Heading” dynamically

One of the known issues of the Dojo mobile is that when the Dojo mobile heading dojox.mobile.Heading has a moveTo attribute, the value of the moveTo attribute cannot be changed once the widget is created. This means that the moveTo attribute will cause the back button to be always going to the initial view id that is binded to the attribute.

In order to resolve this issue (to support, for example, dynamic back navigation), you can destroy the widgets inside the dojox.mobile.Heading and then re-create the widgets again. Let’s see how to do this. The following code snippet shows the mobile heading element in the HTML file:

    <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-attach-point="dynamicHeading">
    <!-- Other elements -->

And in the "onBeforeTransitionIn" of the view, you can call the buildHeadingDynamic("someView") function which destroys the descendants of the heading and then re-create it again with the new moveTo value.

buildHeadingDynamic : function(backView) {
  var dynamicHeading = this.dynamicHeading;

  var heading = new dojox.mobile.Heading ({label:"Heading", fixed:"top", back:"Back", moveTo:"#" + backView});

Overriding Android back button behavior in Apache Cordova

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