Ionic 3 Android设备后退按钮正在退出应用程序

时间:2019-04-11 06:22:32

标签: android cordova ionic3 cordova-plugins back-button

在单击“ Android后退”按钮时,应用程序正在退出。我正在尝试通过registerBackButtonAction处理它,但它本身并未被调用。如果我使用没有任何cordova插件的空白离子应用程序,那么它可以正常工作,并且我可以听registerBackButtonAction。我怀疑某些cordova插件与Android后退按钮冲突。这是我正在使用的插件列表,任何帮助将不胜感激。

"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"
]

1 个答案:

答案 0 :(得分:0)

转到您的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();
}