1

In my ionic App, I used the double press back button(Hardware) to exit the app, in my Homepage.

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);
}

when I open the app and press the back the button, it works properly. if I goto another page and back to home, and I tried back button, nothing happens.

in the another page i used like this

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

  }

How can I use it correctly? pls, help...

faizy
  • 504
  • 5
  • 17

2 Answers2

0

I already face same problem I give ref link.

You have to remove 0 in your code

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

Why we have not give any number because higher value only exit. If your not give any number, it exit all pages. you should follow the above link. It will help you.

EDISON J
  • 324
  • 3
  • 12
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();
              }
          }            
      });

Check This It Works Perfectly 100%

Mohamed Arshath
  • 423
  • 5
  • 15