Last active
August 29, 2015 14:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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