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)){
e.preventDefault();
navigator.app.exitApp();
} else {
history.back();
}
}, 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.
Reference:
“JavaScript Mobile Application Development” Book:
Amazon: http://www.amazon.com/JavaScript-Mobile-Application-Development-Hazem/dp/1783554177
Packtpub: https://www.packtpub.com/web-development/javascript-native-mobile-apps-development