首页 文章

离子应用程序中的后退按钮不起作用

提问于
浏览
1

在我的离子应用程序中,我使用双按下按钮(硬件)退出应用程序,在我的主页中 .

constructor (public navCtrl: NavController,
               public navParams: NavParams,
               private platform: Platform,
............................
 ) {
    this.platform.registerBackButtonAction(() => {
      if (this.counter == 0) {
        this.counter++;
        this.pressAgainToast();
        setTimeout(() => {
          this.counter = 0
        }, 3000)
      } else {
        // console.log("exitapp");
        this.platform.exitApp();
      }
    }, 0);
}

当我打开应用程序并按下后面的按钮时,它可以正常工作 . 如果我转到另一页并回到家里,我试了回按钮,没有任何反应 .

在我使用的另一个页面中

constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private platform: Platform,
              private view:ViewController) {
     this.platform.registerBackButtonAction(() => {
     this.navCtrl.pop()
     });

  }

我该如何正确使用它?请帮忙......

2 回答

  • 0

    我已经面临同样的问题我给ref link .

    您必须在代码中删除0

    this.platform.registerBackButtonAction(() => {
          if (this.counter == 0) {
            this.counter++;
            this.pressAgainToast();
            setTimeout(() => {
              this.counter = 0
            }, 3000)
          } else {
            // console.log("exitapp");
            this.platform.exitApp();
          }
        }, 0);
    

    为什么我们没有给出任何数字,因为更高的 Value 只会退出如果你不给任何号码,它会退出所有页面 . 你应该按照上面的链接 . 它会帮助你 .

  • 0
    //Check Hardware Back Button Double Tap to Exit And Close Modal On Hardware Back
          let lastTimeBackPress = 0;
          let timePeriodToExit  = 2000;
          this.platform.registerBackButtonAction(() => {
              let activePortal = this.ionicApp._loadingPortal.getActive() || // Close If Any Loader Active
              this.ionicApp._modalPortal.getActive() ||  // Close If Any Modal Active
              this.ionicApp._overlayPortal.getActive(); // Close If Any Overlay Active
              if (activePortal) {
                  activePortal.dismiss();
              }
              else if(this.nav.canGoBack()){
                this.nav.pop();
              }else{
                  //Double check to exit app
                  if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
                      this.platform.exitApp(); //Exit from app
                  } else {
                    this.toast.create("Press back button again to exit");
                    lastTimeBackPress = new Date().getTime();
                  }
              }            
          });
    

    检查这是完美的100%

相关问题