Is there a way to activate the device back button within the Ionic framework?

I disabled the back button under certain conditions by registering an action event like this:

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

Now, how can I re-enable the device back button? It is currently still disabled and not allowing me to go back to the previous view.

Answer №1

give this a shot

let lastTimeBackPress = 0;
  let timePeriodToExit = 2000;

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

hopefully this solution works for you

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a CORS header issue while working with the Authorization header

Here is the code snippet I am currently working with: https://i.stack.imgur.com/DYnny.png Removing the Authorization header from the headers results in a successful request and response. However, including the Authorization header leads to an error. http ...

How to interact with AngularJS drop-down menus using Selenium in Python?

I have been working on scraping a website to create an account. Here is the specific URL: Upon visiting the site, you need to click on "Dont have an account yet?" and then click "Agree" on the following page. Subsequently, there are security questions th ...