Hardware back button working unexpectedly


#1

I am trying to prevent the hardware from closing the app, Tried several method but all results in the same behavior.

The current behavior is that the app, closes before executing the expected action.

var lastTimeBackPress = 0;
    var timePeriodToExit = 2000;

    this.platform.registerBackButtonAction(() => {
      // get current active page
      let view = this.navCtrl.getActive();
      if (view.component.name == "TabsPage") {
        //Double check to exit app
        if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
          this.backgroundMode.moveToBackground()//Exit from app
        } else {
          this.util.presentToast("Press back again to exit App?");
          lastTimeBackPress = new Date().getTime();
        }
      } else {
        // go to previous page
        this.navCtrl.pop({});
      }
    });

This is the current code, What happens with this is that. I am on some other page like a third layer, When I pressback it should pop but insted the app closes(goto background) But when I re-open the app it shows the toast “Press back again to exit App?”

Is there a way to disable the back button from functioning for the app?

 document.addEventListener('backbutton', (e) => {
        e.preventDefault();
        if (!this.navCtrl.canGoBack()) {
          this.backgroundMode.moveToBackground();
        }
        this.navCtrl.pop()
      }, false);

This code also closes the app then when I re-open it then pop then current page