1

设备后退按钮 (platform.registerBackButtonAction) 不适用于应用程序后退按钮,也不适用于设备后退按钮。

有人能帮助我吗?

我该如何解决我的问题?

代码:-

  ionViewDidLoad() {
    this.navBar.backButtonClick = (e: UIEvent) => {
      const alert = this.alertCtrl.create({
        title: 'App termination',
        message: 'Do you want to close the app?',
        buttons: [{
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Application exit prevented!');
          }
        }, {
          text: 'Close App',
          handler: () => {
            this.platform.exitApp(); // Close this application
          }
        }]
      });
      alert.present();
    }
  }
4

1 回答 1

0

按以下方式修改您的app.component.ts文件,

  import { Platform, IonicApp } from 'ionic-angular';

  constructor(public platform: Platform, private ionicApp: IonicApp){}

  initializeApp() {
    this.platform.ready().then(() => {
      //back button handle
      this.platform.registerBackButtonAction(() => {
        let activePortal = this.ionicApp._loadingPortal.getActive() ||
          this.ionicApp._modalPortal.getActive() ||
          this.ionicApp._toastPortal.getActive() ||
          this.ionicApp._overlayPortal.getActive();

        if (activePortal) {
          activePortal.dismiss();
        }
        else {
          if (this.nav.canGoBack()) {
            this.nav.pop();
          } else {
            this.showAlert();
          }
        }
      });
    });
  }

  showAlert() {
    let confirm = this.alertCtrl.create({
      title: 'Exit Application?',
      message: 'Do you want to exit this application?',
      buttons: [
        {
          text: 'No',
          handler: () => {}
        },
        {
          text: 'Yes',
          handler: () => {
            navigator['app'].exitApp();
          }
        }
      ]
    });
    confirm.present();
  }
于 2018-07-04T05:19:21.043 回答