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

**URL:** <https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644>\
**Category:** ionic-v3\
**Created:** [March 29, 2017, 10:37am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644 "2017-03-29T10:37:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityajaiz](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@adityajaiz](https://forum.ionicframework.com/u/adityajaiz)\
**Post date:** [March 29, 2017, 10:37am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/1 "2017-03-29T10:37:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maninak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/maninak/32/104399_2.png) [@maninak](https://forum.ionicframework.com/u/maninak)\
**Post date:** [March 29, 2017, 10:50am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/2 "2017-03-29T10:50:35Z")

</div>

See if this [reply](https://forum.ionicframework.com/t/how-to-close-application-using-button-ionic-2/48050/6?u=maninak) and the those after it, are of any help to you.

---

<div class="post-metadata">

**Author:** ![adityajaiz](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@adityajaiz](https://forum.ionicframework.com/u/adityajaiz)\
**Post date:** [March 29, 2017, 11:41am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/3 "2017-03-29T11:41:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maninak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/maninak/32/104399_2.png) [@maninak](https://forum.ionicframework.com/u/maninak)\
**Post date:** [March 29, 2017, 12:55pm UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/4 "2017-03-29T12:55:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adityajaiz](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@adityajaiz](https://forum.ionicframework.com/u/adityajaiz)\
**Post date:** [March 29, 2017, 2:51pm UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/5 "2017-03-29T14:51:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maninak](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/maninak/32/104399_2.png) [@maninak](https://forum.ionicframework.com/u/maninak)\
**Post date:** [March 29, 2017, 3:24pm UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/6 "2017-03-29T15:24:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adityajaiz](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@adityajaiz](https://forum.ionicframework.com/u/adityajaiz)\
**Post date:** [March 30, 2017, 6:44am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/7 "2017-03-30T06:44:23Z")

</div>

This is not working with that Particular Page

---

<div class="post-metadata">

**Author:** ![rgjatin](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@rgjatin](https://forum.ionicframework.com/u/rgjatin)\
**Post date:** [January 6, 2018, 7:16am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/8 "2018-01-06T07:16:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![conceps](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/conceps/32/115624_2.png) [@conceps](https://forum.ionicframework.com/u/conceps)\
**Post date:** [January 6, 2018, 7:33am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/9 "2018-01-06T07:33:47Z")

</div>

Hello, Try This Code

```auto

 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…

---

<div class="post-metadata">

**Author:** ![gokujy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gokujy/32/117753_2.png) [@gokujy](https://forum.ionicframework.com/u/gokujy)\
**Post date:** [October 10, 2018, 10:42am UTC](https://forum.ionicframework.com/t/how-to-exit-app-with-device-back-button-from-a-particular-page/84644/10 "2018-10-10T10:42:54Z")

</div>

Thanks for the code…working perfect
