首页 文章

退出应用程序使用硬件后退按钮从特定页面使用IONIC 3开发

提问于
浏览
1

我尝试使用硬件后退按钮从特定页面( 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({});
    }
  });

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

if(view.component.name =='SignInPage')

但我尝试“ HometabsPage " instead of " SignInPage ”之后,所有页面都显示吐司信息 .

enter image description here

请帮我 .

2 回答

  • 2

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

    /* 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();
          }
        }
    

    }

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

  • -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({});
                }
            });
        });
    }
    

相关问题