ionic 实现双击返回键退出应用功能
ionic 实现双击返回键退出应用功能
keywords
cordova,ngcordova,phonegap,ionic,双击,返回键,退出
例子
准备Toast插件,提示用,用户体验好点,不是必须要装
cordova plugin add cordova-plugin-x-toast
示例:
.run(function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory, $cordovaToast) {
//双击退出
$ionicPlatform.registerBackButtonAction(function (e) {
//判断处于哪个页面时双击退出
//if ($location.path() == '/app/projects' || $location.path() == '/app/plans') {
if($state.includes('app.*'))
if ($rootScope.backButtonPressedOnceToExit) {
ionic.Platform.exitApp();
} else {
$rootScope.backButtonPressedOnceToExit = true;
$cordovaToast.showShortTop('再按一次退出系统');
setTimeout(function () {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000);
}
}else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
$rootScope.backButtonPressedOnceToExit = true;
$cordovaToast.showShortTop('再按一次退出系统');
setTimeout(function () {
$rootScope.backButtonPressedOnceToExit = false;
}, 2000);
}
e.preventDefault();
return false;
}, 101);
}
参考: