Ionic在android中返回键的处理问题
作者:互联网
一,一段代码就可以解决问题,这段代码应该放在app.js中:
//ionic点击系统返回键退出APP
.run(function ($rootScope, $ionicPlatform, $state, $ionicHistory, $ionicPopup, $cordovaKeyboard, $timeout) {
window.addEventListener('native.keyboardhide', function (e) {
cordova.plugins.Keyboard.isVisible = true;
$timeout(function () {
cordova.plugins.Keyboard.isVisible = false;
}, 100);
});
$ionicPlatform.registerBackButtonAction(function (e) {
//阻止默认的行为
e.preventDefault();
// 退出提示框
function showConfirm() {
var servicePopup = $ionicPopup.show({
title: '提示',
subTitle: '你确定要退出应用吗?',
scope: $rootScope,
buttons: [
{
text: '取消',
type: 'button-clear button-calm',
onTap: function () {
return 'cancel';
}
},
{
text: '确认',
type: 'button-clear button-calm border-left',
onTap: function (e) {
return 'active';
}
}
]
});
servicePopup.then(function (res) {
if (res == 'active') {
// 退出app
ionic.Platform.exitApp();
}
});
}
// 判断当前路由是否为各个导航栏的首页,是的话则显示提示框
var current_state_name = $state.current.name;
if ($cordovaKeyboard.isVisible()) {
$cordovaKeyboard.close();
} else {
if (current_state_name == 'login' || current_state_name == 'tab.dash' || current_state_name == 'tab.positons' || current_state_name == 'tab.account') {
showConfirm();
} else if ($ionicHistory.backView()) {
$ionicHistory.goBack();
} else {
showConfirm();
}
}
}, 402); //101优先级常用于覆盖‘返回上一个页面’的默认行为
})
二、下载上述代码中所需要的插件$cordovaKeyboard即可:
npm安装命令如下
cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.git
注:应该注意的是:本问题出现的原因是因为返回键和ionic中的控件优先级出现了矛盾,望注意!有什么问题,欢迎指正
标签:返回,function,ionic,name,button,current,state,android,Ionic 来源: https://blog.51cto.com/u_15198640/2772000