如果使用了
this.app.getRootNav().push()以及this.navCtrl.push();
则在注册安卓返回键的时候
registerBackButtonAction() {
if (!this.nativeService.isAndroid()) {
return;
}
this.platform.registerBackButtonAction(() => {
if (this.keyboard.isOpen()) {//如果键盘开启则隐藏键盘
this.keyboard.close();
return;
}
//点击返回按钮隐藏toast或loading或Overlay
this.ionicApp._toastPortal.getActive() ||this.ionicApp._loadingPortal.getActive()|| this.ionicApp._overlayPortal.getActive();
//隐藏modal
let activePortal = this.ionicApp._modalPortal.getActive();
if (activePortal) {
activePortal.dismiss();
return;
}
//页面返回
if(this.app.getRootNav().canGoBack()){
//this.showExit() this.nativeService.minimize()
return this.app.goBack()
}else{
return this.showExit();
}
}, 10);
}
//双击退出提示框
showExit() {
if (this.backButtonPressed) { //当触发标志为true时,即2秒内双击返回按键则退出APP
this.platform.exitApp();
} else {
this.nativeService.showToast('再按一次退出应用');
this.backButtonPressed = true;
setTimeout(() => { //2秒内没有再次点击返回则将触发标志标记为false
this.backButtonPressed = false;
}, 2000)
}
}