ionic3 安卓硬件返回

platform.ready().then(() => {

      this.platform.registerBackButtonAction(() => {

        let activePortal = this.ionicApp._modalPortal.getActive() ||this.ionicApp._overlayPortal.getActive();
        console.log(activePortal)
        let loadingPortal = this.ionicApp._loadingPortal.getActive();
        if (activePortal) {
//其他的关闭 activePortal.dismiss().catch(() => { }); activePortal.onDidDismiss(() => { }); return; } if (menuCtrl.isOpen()) { 
menuCtrl.close(); console.log("closing menu"); return; } if (loadingPortal) { //loading的话,返回键无效 return; }
//获取NavController if(this.keyboard.isOpen()){ this.keyboard.close(); return; }
let activeNav: NavController = this.appCtrl.getActiveNav();
console.log(this.appCtrl); console.log(activeNav)
//如果可以返回上一页,则执行pop
if (activeNav.canGoBack()) { activeNav.pop(); } else {
//执行退出 this.showExit();
} });
});
  private alertT(content){
    this.alert = this.alertCtr.create({
      // spinner: 'hide',
      title:''  ,
      enableBackdropDismiss:false,
      // duration: 3000
    });
    this.alert.present();
  }
  //退出应用方法
  private showExit(): void {
    //如果为true,退出
    if (this.backButtonPressed) {
      this.platform.exitApp();
    } else {
      //第一次按,弹出Toast
      if (localStorage.getItem("language") === 'zh') {
        this.commonUtils.showToast("再按一次退出应用", 2000)
      } else {
        this.commonUtils.showToast("Press again to exit the app", 2000)
      }
      // this.toastCtrl.create({
      //     message: '再按一次退出应用',
      //     duration: 2000,
      //     position: 'bottom'
      // }).present();
      //标记为true
      this.backButtonPressed = true;
      //两秒后标记为false,如果退出的话,就不会执行了
      setTimeout(() => this.backButtonPressed = false, 2000);
    }
  }

主要解决的问题是:modal 的关闭

代码:

let activePortal = ionicApp._loadingPortal.getActive() ||
               ionicApp._modalPortal.getActive() ||
               ionicApp._toastPortal.getActive() ||
               ionicApp._overlayPortal.getActive();

            if (activePortal) {
               ready = false;
               activePortal.dismiss();
               activePortal.onDidDismiss(() => { ready = true; });

               Logger.log("handled with portal");
               return;
            }

            if (menuCtrl.isOpen()) {
               menuCtrl.close();

               Logger.log("closing menu");
               return;
            }

  

  另外本来 这块内容是写再指令里的发现 指令里不能用ionicApp 这个方法,最好把内容放到初始化位置。

原文地址:https://www.cnblogs.com/wupeng88/p/9951252.html