How can i enable device backbutton in ionic?

2019-09-17 06:44发布

问题:

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:

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