Skip to content

Instantly share code, notes, and snippets.

@javierarques
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierarques/adc8ef536bcc0957b24c to your computer and use it in GitHub Desktop.
Save javierarques/adc8ef536bcc0957b24c to your computer and use it in GitHub Desktop.
Cordova - Angular app - Exit app clicking back button if there is no history back
// FACTORY ____________________________________________________________________________________________________________
/**
@ngdoc factory
@name cordova.factory:cordovaExitApp
@description Adds events to exit app on Android
http://stackoverflow.com/questions/18184497/backbutton-confirm-exit-app-android-phonegap-jquery
*/
// DEFINITION _________________________________________________________________________________________________________
function cordovaExitApp($window) {
var self = this;
this.onBackKeyDown = function(e) {
//e.preventDefault();
// if there's no history back
if (parseInt($window.history.length) <= 2 ) {
// Prompt the user with the choice
//navigator.notification.confirm("¿Seguro que quieres salir de sportmaniacs?", self.onConfirm, "Salir", "Sí,No");
navigator.app.exitApp();
} else {
$window.history.back();
}
};
this.init = function() {
document.addEventListener("backbutton", self.onBackKeyDown, false); //Listen to the User clicking on the back button
};
this.onConfirm = function(button) {
if(button ===2 ) {
//If User selected No, then we just do nothing
return;
}else {
// Otherwise we quit the app.
navigator.app.exitApp();
}
};
return {
init: self.init
};
}
// MODULE ASSIGNEMENT _________________________________________________________________________________________________
angular.module('cordova')
.factory('cordovaExitApp', cordovaExitApp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment