I want to show a exit app confirm modal on login or home page when user press android hardware back button, but on other pages just back to previous page, I have trouble in the funtion, I know $ionicPlatform.registerBackButtonAction , but how can I know current location?
I have wrote blow code in my LoginControl , it can show confirm modal on login and home page , but can not back to previous in other page.
var exitConfirm = function () {
if ($location.path() == '/login' || $location.path() == '/main/recharge') {
$ionicPopup.confirm({
title: 'Exit App',
content: 'Do you want to exit?',
okText: 'OK',
cancelText: 'Cancel'
}).then(function (res) {
if (res) {
navigator.app.exitApp();
}
});
}
}
var removeConfirm = angular.noop();
if ($location.path() == '/login' || $location.path() == '/main/recharge') {
removeConfirm = $ionicPlatform.registerBackButtonAction(
exitConfirm,
100, 111111111111
);
} else {
removeConfirm();
}
$ionicPlatform.registerBackButtonAction(function(e) {
e.preventDefault();
function showConfirm() {
var confirmPopup = $ionicPopup.confirm({
title: '<strong>Exit X-Wing Companion?</strong>',
template: 'Are you sure you want to exit XWC?'
});
confirmPopup.then(function(res) {
if (res) {
ionic.Platform.exitApp();
} else {
// Don't close
}
});
}
// Is there a page to go back to?
if ($rootScope.$viewHistory.backView) {
// Go back in history
$rootScope.$viewHistory.backView.go();
} else {
// This is the last page: Show confirmation popup
showConfirm();
}
return false;
}, 101);
I put this in the .run() call in app.js. Make sure to add $ionicPopup and $ionicPlatform as arguments, like so: .run(function($rootScope, $ionicPopup, $ionicPlatform) {
@offbye It’s not working for me
It disable the back button but not show confirm.
.run(function($ionicPlatform, $ionicPopup, $rootScope, $location) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleLightContent();
}
//主页面显示退出提示框
$ionicPlatform.registerBackButtonAction(function(e) {
e.preventDefault();
function showConfirm() {
var confirmPopup = $ionicPopup.confirm({
title: '<strong>退出应用?</strong>',
template: '你确定要退出应用吗?',
okText: '退出',
cancelText: '取消'
});
confirmPopup.then(function(res) {
if (res) {
ionic.Platform.exitApp();
} else {
// Don't close
}
});
}
// Is there a page to go back to?
if ($location.path() == '/tab/locations') {
showConfirm();
} else if ($rootScope.$viewHistory.backView) {
console.log('currentView:', $rootScope.$viewHistory.currentView);
// Go back in history
$rootScope.$viewHistory.backView.go();
} else {
// This is the last page: Show confirmation popup
showConfirm();
}
return false;
}, 101);
});
})