Exit application from android hardware back button in phonegap
Solution 1
Try this:
document.addEventListener('backbutton', function(event){
event.preventDefault(); // EDIT
navigator.app.exitApp(); // exit the app
});
I hope it helps you.
Solution 2
It will ask you before exit in stipulated timestamp : Press back button again to exit. If it's dashboard page then it will ask whether to Log out? I hope it will solve your problem.
.run(function($rootScope, $ionicPlatform, $ionicHistory){
$ionicPlatform.registerBackButtonAction(function(e) {
if ($ionicHistory.currentView().stateName == 'dashboard') {
Dialogs.showConfirm('Do you want to logout?', function(buttonIndex) {
if (buttonIndex == 1)
User.clear();
}, 'Logout', ['Yes', 'No']);
} else
if ($rootScope.backButtonPressedOnceToExit) {
ionic.Platform.exitApp();
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
window.plugins.toast.showShortCenter(
"Press back button again to exit",
function(a) {},
function(b) {}
);
setTimeout(function() {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000);
}
e.preventDefault();
return false;
}, 101);
});
Solution 3
The following close the app if we hit back button on Android device in "Login" screen.
In your app.js you maybe have something like this:
app.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$stateProvider.state('login', {
url : '/',
}).state('login', {
url : '/login',
views : {
contentView : {
templateUrl : 'login.html',
controller : 'LoginCtrl'
}
}
});
So all you have to do is add this method in the same file:
app.run(function($ionicPlatform) {
$ionicPlatform.registerBackButtonAction(function() {
if ($state.current.name == "login") {
navigator.app.exitApp();
}
}, 100);
});
Note that "login" it the state name which mentioned in the app.config
CraZyDroiD
Updated on June 05, 2022Comments
-
CraZyDroiD almost 2 years
Is there a way to show a confirmation popup when you press the hardware back button in android devices to exit the phonegap/ionic application?
In the current status, my app keep going to the previous state upon clicking the back button.
Is there a way to exit the app when you press the back button no matter where you are on, in the application?
I found this piece of code but it did not seem to work:
$ionicPlatform.registerBackButtonAction(function() { var confirmPopup = $ionicPopup.confirm({ title: 'Sign Out Confirm', template: 'Are you sure you want to Logout?' }); confirmPopup.then(function(res) { if (res) { $rootScope.rootScopeUserTransactionPassword = null; $state.go('app.playlists'); } else { console.log('You are not sure'); } }); }, 100);
-
Anand Gupta over 9 yearsInside the head tag of your index.html file you can place this code inside <script></script> tag.
-
Mohammed Imran N over 9 yearsthis code will make you to get out of app from all the screens. you gotta do some modifications, if you dont wanna get out of app except home screen.
-
Anand Gupta over 9 yearsCheck the answer. I have added one line. Tell me if it is not helping.
-
Prashanth VG over 7 yearsHow to write this if I am doing this using Cordova and not ionic?