退出App使用硬件后退按钮

时间:2017-10-24 06:58:45

标签: ionic-framework ionic2 ionic3 back-button-control

我尝试使用硬件后退按钮从特定页面( HometabsPage )退出应用。 我使用下面的代码:

  var lastTimeBackPress = 0;
  var timePeriodToExit = 2000;

  platform.registerBackButtonAction(() => {
    let view = this.nav.getActive();
    if (view.component.name == 'SignInPage' ) {
      if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
        platform.exitApp(); //Exit from app
      } else {
        this.common.presentToast("Press back again to exit App?", "bottom");
        lastTimeBackPress = new Date().getTime();
      }
    } else {
      this.nav.pop({});
    }
  });

在我的应用程序中,有两个部分 SignIn Hometabs 。上面的代码在 SignIn 页面上正常工作。

  

if(view.component.name ==&#39; SignInPage&#39;)

但我尝试&#34; HometabsPage &#34;而不是&#34; SignInPage &#34;之后在所有页面中显示吐司信息。

enter image description here

请帮帮我。

2 个答案:

答案 0 :(得分:2)

@Neotrixs登录后,将 HomeTabsPage 设置为根页。它会阻止您的应用返回登录页 对于硬件后退按钮,我通过以下方法完成:

/* REGISTERING BACK BUTTON TO HANDLE HARDWARE BUTTON CLICKED */
  registerBackButton(){
    let backButton = this.platform.registerBackButtonAction(() => {
      var stackSize = this.nav.length();
      if(stackSize < 1)
        this.askForPressAgain();
      else
        this.nav.pop();  
    },1);

  }

  /*ASKING FOR PRESS BACK BUTTON AGAIN*/
  askForPressAgain(){
    let view = this.nav.getActive();
    if (view.component.name == 'ProjectsPage' || view.component.name == 'LoginPage') {
      if ((new Date().getTime() - this.lastTimeBackPress) < this.timePeriodToExit) {
        this.platform.exitApp(); //Exit from app
      } else {
        this.toast.showBottomToast(BACK_BTN_MESSAGE);
        this.lastTimeBackPress = new Date().getTime();
      }
    }

}

在上面的代码中,我首先检查了堆栈大小,如果小于1,则显示Toast以确认离开应用程序。
希望它能帮助你或其他人。

答案 1 :(得分:-1)

Ionic最新版本3.xx

app.component.ts文件:

import { Platform, Nav, Config, ToastController } from 'ionic-angular';

constructor(public toastCtrl: ToastController, public platform: Platform) {
    platform.ready().then(() => {
        //back button handle
        //Registration of push in Android and Windows Phone
        var lastTimeBackPress = 0;
        var timePeriodToExit  = 2000;

        platform.registerBackButtonAction(() => {
            // get current active page
            let view = this.nav.getActive();
            if (view.component.name == "TabsPage") {
                //Double check to exit app
                if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
                    this.platform.exitApp(); //Exit from app
                } else {
                    let toast = this.toastCtrl.create({
                        message:  'Press back again to exit App?',
                        duration: 3000,
                        position: 'bottom'
                    });
                    toast.present();
                    lastTimeBackPress = new Date().getTime();
                }
            } else {
                // go to previous page
                this.nav.pop({});
            }
        });
    });
}