How to EXIT App with device back button from a Particular Page?

I want to EXIT the App from Particular Page in IONIC2 , Here is my Code-

 this.platform.ready().then(() => {
platform.registerBackButtonAction(() => 

      if(this.navCtrl.canGoBack()){
        this.navCtrl.pop();
      }else{
          this.showAlert();
      }
    });
}

See if this reply and the those after it, are of any help to you.

Can you please share some code, It would be helpful for me

Sure, I think you could use something like this (of course this works only on Android since there is no hardware “back” button on iOS devices):

import { Platform } from 'ionic-angular';

@Component({ /*...*/ })
export class MyComponent {

  constructor(platform: Platform) {
    platform.ready().then(() => {
      platform.registerBackButtonAction(() => {
        platform.exitApp();
      });
    });
  }

}

I haven’t tested the code myself but you should be able to pick it up from here.

1 Like

Thanks for Sharing but I want to Exit from a Particular page of the App

I’m not sure what I’m missing here. Given that each page in Ionic is a component, then adding the above constructor code in your page’s constructor should work.

This is not working with that Particular Page

it is not work for me . my issue is when pressed on backbutton then it exit from app , i want to set on a perticular page

Hello, Try This Code


 platform.ready().then(() => {
                 //back button handle
                 //Registration of push in Android and Windows Phone
                 var lastTimeBackPress = 0;
                 var timePeriodToExit  = 2000;
                 platform.registerBackButtonAction(() => {
                   let ref = this.viewCtrl.pageRef();
                     console.log('Check your needed page ref name', ref.nativeElement.localName) 
                     if(ref.nativeElement.localName =='page-no-internet'){
                         //Double check to exit app
                          if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
                              this.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{
                          this.nav.pop();
                     }
               });
}

Enjoy…

1 Like

Thanks for the code…working perfect