I am trying to exit from the app on soft-back-button press from home screen. Following code inside app.js does not work -
angular.module('starter', ['ionic', 'MyController'] )
.run(function($ionicPlatform, $ionicHistory, $ionicPopup) {
$ionicPlatform.ready(function() {
$ionicPlatform.registerBackButtonAction(function(event) {
var currentState = $ionicHistory.currentStateName();
if (currentState == 'tab.business' || currentState == 'tab.personal') {
console.log('About to exit the app');
ionic.Platform.exitApp();
navigator.app.exitApp(); // none works
} else {
$ionicHistory.goBack();
}
}, 101);
});
Please help
1 Like
âClosingâ the app in android ⌠closes the current process, but it gets not removed from the open process list. But if you click on the app preview in the process list you app should restart. Can you check this?
Looks like the app process does not get closed. When re-opning from the background app list, it just resumes. I feel like nothing executed when I call the app exit method. What could be missing ?
Ok that shows a warning in the debugger- âScripts may close only the windows that were opened by it.â, but tried that as well and no success ![:frowning: :frowning:](https://emoji.discourse-cdn.com/twitter/frowning.png?v=5)
This post is little old, but just wanted to share one solution, if it helps someone else. I am able to exit the app after adding a separate cordova plugin : https://www.npmjs.com/package/cordova-plugin-exitapp
Somehow the ionic.Platform.exitApp() or navigator.app.exitApp() was not working for my ionic app, may be the clobber target for exit app function was not merged correctly as I did lot of plugin removal and addition operations. I donât know the exact reason, It would be great if someone can answer what caused the problem.
My steps to solve it:
add the plugin : cordova plugin add cordova-plugin-exitapp
Then add the following in your app.js :
angular.module('XYZApp', ['ionic'])
.run(function($ionicPlatform, $ionicHistory, $rootScope) {
$ionicPlatform.ready(function() {
// For exit on back button press
$ionicPlatform.registerBackButtonAction(function(e) {
if ($rootScope.backButtonPressedOnceToExit) {
navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
// "Press back button again to exit" : show toast
setTimeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
}
e.preventDefault();
return false;
}, 101);
});
}
Thanks,
1 Like
Sorry, but how to you use
currentState == 'tab.business' || currentState == 'tab.personal
When you use the plugin
@PterPmnta, Sorry, could you please rephrase that , I didnât understand.
I have the same problem that you exposed in your home , when in a specific view, in my case would be a menu, when you press the button back on android , exit the application
This is my code
`angular.module(âunicesarAppâ, [âionicâ, ângCordovaâ])
.run(function($ionicPlatform, $ionicHistory, $rootScope, $cordovaToast, $timeout) {
var vista;
$ionicPlatform.ready(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
// For exit on back button press
$ionicPlatform.registerBackButtonAction(function(e) {
if ($ionicHistory.currentStateName() == 'menuestu' || $ionicHistory.currentStateName() == 'menuprof') {
navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
$cordovaToast.show('Presione para salir de la aplicacion', 'long', 'center');
$timeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
}
e.preventDefault();
return false;
}, 101);
});
});
`
But dont work for me
Yes @PterPmnta , thatâs the reason I chose this plugin to make it work : https://www.npmjs.com/package/cordova-plugin-exitapp8
And follow the steps IU mentioned in my post above. Should work.
i solved this way
`angular.module(âhistorialAppâ, [âionicâ])
.controller(âborrarHistorialâ, borrarHistorial)
.factory(âsalirAppâ, salirApp);
borrarHistorial.$inject = [â$ionicHistoryâ, âsalirAppâ];
function borrarHistorial($ionicHistory, salirApp){
salirApp.salida();
$ionicHistory.clearHistory();
};
salirApp.$inject = [â$ionicPlatformâ,â$ionicHistoryâ, â$timeoutâ];
function salirApp ($ionicPlatform , $ionicHistory, $timeout) {
function salida(){
var BackButton = 0;
$ionicPlatform.registerBackButtonAction(function () {
if ($ionicHistory.currentStateName() == 'menuestu' || $ionicHistory.currentStateName() == 'menuprof') {
if (BackButton == 0) {
BackButton++;
window.plugins.toast.showLongCenter('Presione nuevamente para salir');
$timeout(function() {
BackButton = 0;
}, 2500);
}else{
navigator.app.exitApp();
}
}else{
$ionicHistory.backView();
}
}, 100);
}
return {
salida: salida
};
};
`
.run(function($ionicPlatform,$state,$ionicHistory) {
$ionicPlatform.registerBackButtonAction(function (event) {
if ($state.current.name==âwelcomeâ || $state.current.name==âapp.homeâ){
$ionicHistory.clearCache();
navigator.app.exitApp();
}
else {
//navigator.app.backHistory();
$ionicHistory.goBack();
}
event.preventDefault();
return false;
}, 101);
})
Use this code , this works in my case⌠Try it.