How can i enable device backbutton in ionic?

2019-09-17 07:17发布

I have disabled backbutton for some condition by backbutton register action event like this:

    $ionicPlatform.registerBackButtonAction(function (event) {
    if (condition)
       {
       event.preventDefault();
       $ionicHistory.nextViewOptions({ disableBack: true });
       } 
    else
       {
       $ionicHistory.goBack();
       }
       }, 800);

So now how can i enable that device backbutton again ? Because its still disabled and not going in previous view too.

1条回答
Evening l夕情丶
2楼-- · 2019-09-17 07:46

you need to try this

var lastTimeBackPress = 0;
  var timePeriodToExit = 2000;

  platform.registerBackButtonAction(() => {
    // get current active page
    let view = this.nav.getActive();
    if (view.component.name == "HomePage") {
      //Double check to exit app                  
      if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
        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({});
    }
  });

hope it will work for you

查看更多
登录 后发表回答