I'm using "backbutton" event of Cordova (version 6.3.2) in my Android app. But when the back button is clicked, the app closes instead of triggering the backbutton event. There are many similar questions but I had to ask again as nothing helped me.
My Init Script :
var cordovaInit = function (){
//To bootstrap the app manually once the device is ready
var receivedEvent = function (){
angular.bootstrap($('body'), ['myModule']);
};
var onDeviceReady = function (){
console.log("Device Ready");
receivedEvent('deviceReady');
document.addEventListener("backbutton", function(e){
// My Code here
});
};
//call onDeviceReady when the device is ready
this.bindEvents = function (){
document.addEventListener('deviceReady', onDeviceReady, false);
};
//If cordova is present, wait for it to initialize, otherwise just try to
//bootstrap the application.
if(window.cordova !== undefined){
this.bindEvents();
}
else{
receivedEvent();
}
};
$(function (){
new cordovaInit();
});
As you can see above that I'm registering the event after the deviceReady event and the deviceReady event is working fine. Any help is very much appreciated. Thanks in advance.
I think what might be happening is your app has one screen and when the back button is pressed it quits the app. You need to prevent the default back behavior. Try adding this line inside the back button event: