点击Android Back按钮后,应用即将退出。我试图通过registerBackButtonAction
来处理它,但它本身并没有被调用。如果我使用没有任何cordova插件的空白离子应用程序,它的工作正常,我可以听registerBackButtonAction
。我怀疑一些cordova插件与Android Back按钮发生冲突。以下是我正在使用的插件列表,任何帮助将不胜感激。
"plugins": {
"cordova-plugin-whitelist": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-device": {},
"cordova-plugin-splashscreen": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
},
"cordova-plugin-ionic-keyboard": {},
"cordova-plugin-media-capture": {},
"cordova-plugin-file": {},
"cordova-plugin-camera": {},
"cordova-plugin-streaming-media": {},
"cordova-plugin-local-notification": {},
"mx.ferreyra.callnumber": {},
"call-number": {},
"cordova.plugins.diagnostic": {
"ANDROID_SUPPORT_VERSION": "28.+"
},
"cordova-android-support-gradle-release": {
"ANDROID_SUPPORT_VERSION": "27.+"
},
"cordova-plugin-audio-recorder": {},
"cordova-plugin-network-information": {}
},
"platforms": [
"android"
]
转到app.component.ts并添加此代码
platformReady(){ // back button functionality.
this.platform.ready().then(() => {
this.statusBar.styleDefault();
setTimeout(() => {
this.splashScreen.hide();
}, 100);
this.platform.registerBackButtonAction(()=>this.myHandlerFunction());
});
}
myHandlerFunction(){
let nav = this.app._appRoot._getActivePortal() || this.app.getActiveNav();
let activeView = nav.getActive();
if (activeView != null) {
if (nav.canGoBack()) {
nav.pop();
} else if(activeView.isOverlay) {
activeView.dismiss();
} else {
this.showExitAlert();
}
}
}
showExitAlert() {
this.alertCtrl.create({
title: 'app name',
cssClass:'alert-danger',
message: 'Are you sure you want to exit the app?',
enableBackdropDismiss: false,
buttons: [{ text: 'Yes', handler: () => {
this.platform.exitApp();
}
}, { text: 'Cancel', role: 'cancel', handler: () => {
}
}]
}).present();
}