# Android hardware back button in ionic

**URL:** <https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522>\
**Category:** ionic-v3\
**Created:** [February 23, 2016, 10:41am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522 "2016-02-23T10:41:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammedabuali](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/muhammedabuali/32/97330_2.png) [@Muhammedabuali](https://forum.ionicframework.com/u/Muhammedabuali)\
**Post date:** [February 23, 2016, 10:41am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/1 "2016-02-23T10:41:36Z")

</div>

Hello all,  
in the tutorial for ionic 2 [ionic 2 tutorial](http://ionicframework.com/docs/v2/getting-started/tutorial) after clicking on an item the back button closes the app instead of going back to the list page I tried using platform

`registerBackButtonAction` but it didn’t work. here is the code I tried

```auto
import {Page, NavController, NavParams, Platform} from 'ionic-framework/ionic';
import {Inject} from 'angular2/core';
@Page({
  templateUrl: 'build/pages/item-details/item-details.html'
})
export class ItemDetailsPage {
  constructor(@Inject(NavController) nav, @Inject(NavParams) navParams,
  @Inject(Platform) platform) {
    this.nav = nav;
    // If we navigated to this page, we will have an item available as a nav param
    this.selectedItem = navParams.get('item');
    this.platform = platform;
    console.log(this.platform);
    this.platform.registerBackButtonAction(function(){
    },500);
    // this.platform.onHardwareBackButton(function(e){
    // e.preventDefault();
    // return true;
    // });
  }
}

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [February 24, 2016, 5:37pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/2 "2016-02-24T17:37:24Z")

</div>

Hardware back button support is a feature we have not added yet. It’s on our road map

---

<div class="post-metadata">

**Author:** ![Daveshirman](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@Daveshirman](https://forum.ionicframework.com/u/Daveshirman)\
**Post date:** [February 26, 2016, 12:51am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/3 "2016-02-26T00:51:33Z")

</div>

Is there any approximate timeline for this to be implemented in Ionic 2? At the moment, this makes using Ionic 2 for an Android app much less viable.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [February 26, 2016, 3:00pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/4 "2016-02-26T15:00:14Z")

</div>

You can checkout our public road map [here](https://docs.google.com/document/d/1Qlc5X2eJyOB0izkFlH7KJ5BmMi0MeXUZRHJHt3hS6Wo/edit)

---

<div class="post-metadata">

**Author:** ![Y\_R](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@Y\_R](https://forum.ionicframework.com/u/Y_R)\
**Post date:** [March 8, 2016, 8:35am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/6 "2016-03-08T08:35:13Z")

</div>

I think this should be the most basic feature and important. Android users are used to the back button, and will just stop using the app if it closes on pressing the back button.  
I am considering moving to ionic, and will not publish my app unless I am able to implement this.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [March 8, 2016, 10:03am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/7 "2016-03-08T10:03:57Z")

</div>

This feature already exists in ionic V1. Since V2 is still beta, there’s still some features we need to work on.

---

<div class="post-metadata">

**Author:** ![Daveshirman](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@Daveshirman](https://forum.ionicframework.com/u/Daveshirman)\
**Post date:** [March 14, 2016, 12:05am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/8 "2016-03-14T00:05:26Z")

</div>

I solved it for now. Read my quick blog post on it here:

[http://www.codingandclimbing.co.uk/blog/ionic-2-android-back-button-13](http://www.codingandclimbing.co.uk/blog/ionic-2-android-back-button-13)

Cheers.

---

<div class="post-metadata">

**Author:** ![omar123456789](https://avatars.discourse-cdn.com/v4/letter/o/cdc98d/32.png) [@omar123456789](https://forum.ionicframework.com/u/omar123456789)\
**Post date:** [March 14, 2016, 8:31am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/9 "2016-03-14T08:31:57Z")

</div>

What is the expected due date for beta 3?

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [March 14, 2016, 2:44pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/10 "2016-03-14T14:44:07Z")

</div>

No set due date right now

---

<div class="post-metadata">

**Author:** ![nebrekab](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nebrekab/32/99222_2.png) [@nebrekab](https://forum.ionicframework.com/u/nebrekab)\
**Post date:** [March 15, 2016, 12:54pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/11 "2016-03-15T12:54:29Z")

</div>

Hi

I’m a tad confused, why is there documentation for this:  
[http://ionicframework.com/docs/api/service/$ionicPlatform/#onHardwareBackButton](http://ionicframework.com/docs/api/service/%24ionicPlatform/#onHardwareBackButton)

Are you saying this will not work yet?

Thanks

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [March 15, 2016, 4:34pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/12 "2016-03-15T16:34:22Z")

</div>

@nebrekab So that doc that you have linked to is for Ionic V1. We’re talking about Ionic V2

---

<div class="post-metadata">

**Author:** ![luchillo17](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/luchillo17/32/78209_2.png) [@luchillo17](https://forum.ionicframework.com/u/luchillo17)\
**Post date:** [March 15, 2016, 9:22pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/13 "2016-03-15T21:22:36Z")

</div>

Or you could watch for the document event for back button and use it as you need, for example here’s mine from the `init()` method in the `app.ts` file inside `platform.ready().then(() => {...})`:

```auto
// ------------ Double back button press to exit---------------
      document.addEventListener('backbutton', () => {
        if (this.nav.canGoBack()) {
          this.nav.pop()
          return;
        }
        if(!this.backPressed) {
          this.backPressed = true
          window.plugins.toast.show('Presiona el boton atras de nuevo para cerrar', 'short', 'bottom')
          setTimeout(() => this.backPressed = false, 2000)
          return;
        }
        // this.platform.exitApp()
        navigator.app.exitApp()
      }, false);

```

---

<div class="post-metadata">

**Author:** ![nebrekab](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nebrekab/32/99222_2.png) [@nebrekab](https://forum.ionicframework.com/u/nebrekab)\
**Post date:** [March 16, 2016, 2:32pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/14 "2016-03-16T14:32:01Z")

</div>

Ah! A subtle but important detail 🙂  
Thanks for the reply. I’ll keep an eye on this, for the time being @Daveshirman fix worked.

---

<div class="post-metadata">

**Author:** ![luchillo17](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/luchillo17/32/78209_2.png) [@luchillo17](https://forum.ionicframework.com/u/luchillo17)\
**Post date:** [April 5, 2016, 4:25pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/15 "2016-04-05T16:25:10Z")

</div>

His fix relies on alert while mine in toast notifications, both are valid options, it depends how you want the back button to work.

---

<div class="post-metadata">

**Author:** ![imen\_imen](https://avatars.discourse-cdn.com/v4/letter/i/a8b319/32.png) [@imen\_imen](https://forum.ionicframework.com/u/imen_imen)\
**Post date:** [August 1, 2016, 5:35am UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/16 "2016-08-01T05:35:14Z")

</div>

Hi,  
I have the same problem, i use ionic 2 beta 8 and here is my code:

```
this.platform.ready().then(() => {
       this.hideSplashScreen();
       this.platform.registerBackButtonAction(function(event){
         let nav=this.app.getComponent('nav');
         if (nav.canGoBack()) {nav.pop();}
         else {this.confirmExitApp(nav);}
       },101);
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
   
    });

```

i tried on real device and the hardware back is disabled, it does nothing.  
Any help please. I googled a lot for any example of use of registerBackButtonAction in ionic 2 but no result

---

<div class="post-metadata">

**Author:** ![varshil29](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@varshil29](https://forum.ionicframework.com/u/varshil29)\
**Post date:** [August 12, 2016, 12:47pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/17 "2016-08-12T12:47:27Z")

</div>

hey guzs i also get same issue??any solution came??

---

<div class="post-metadata">

**Author:** ![colinjlacy1](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/colinjlacy1/32/99853_2.png) [@colinjlacy1](https://forum.ionicframework.com/u/colinjlacy1)\
**Post date:** [August 13, 2016, 8:26pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/18 "2016-08-13T20:26:53Z")

</div>

Hey, I just worked through a similar problem that someone posted on my blog. You can find [my answer to his (her?) question here](https://webcake.co/exploring-nav-hierarchy-in-the-ionic-2-tabs-page/#div-comment-148), and the code implemented in a project [on my GitHub](https://github.com/colinjlacy/ionic-2-smart-tabs/blob/master/app/pages/tabs/tabs.ts#L27). Hope it helps.

---

<div class="post-metadata">

**Author:** ![varshil29](https://avatars.discourse-cdn.com/v4/letter/v/49beb7/32.png) [@varshil29](https://forum.ionicframework.com/u/varshil29)\
**Post date:** [August 19, 2016, 12:12pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/19 "2016-08-19T12:12:38Z")

</div>

i write below code for back button handle but nothing happend

platform.registerBackButtonAction(() =\> {  
console.log(‘in app.ts’);  
if(menu.isOpen())  
{  
menu.close();  
}else  
{  
nav1.pop();  
}  
});

i write this in all page. is there any issue to write in all page?

---

<div class="post-metadata">

**Author:** ![arturalkaim](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@arturalkaim](https://forum.ionicframework.com/u/arturalkaim)\
**Post date:** [September 14, 2016, 2:15pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/20 "2016-09-14T14:15:56Z")

</div>

HI,

I’m tring to override the backbutton with:

```
this.platform.registerBackButtonAction(this.goBack);

goBack() {
	this.app.getRootNav().setRoot(Main);
}

```

But I always get:  
`Uncaught TypeError: Cannot read property 'getRootNav' of undefined`

Anyone has seen and solved this?

Thanks

---

<div class="post-metadata">

**Author:** ![reedrichards](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@reedrichards](https://forum.ionicframework.com/u/reedrichards)\
**Post date:** [September 14, 2016, 4:10pm UTC](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522/21 "2016-09-14T16:10:53Z")

</div>

Maybe the app isn’t yet ready? Does it help to test the platform first?

For the record, to override the back button I did the following, works fine for me:

```
 onPageWillEnter() {
      this.platform.ready().then(() => {
        this.unregisterCustomBackActionFunction = this.platform.registerBackButtonAction(() => {
              // The special stuff I want to do and where I want to go
       });
    });
 }

```

and then later, before leaving my view, to set back the back action to her default behavior

```
  this.unregisterCustomBackActionFunction();
```

[Next page](https://forum.ionicframework.com/t/android-hardware-back-button-in-ionic/44522.md?page=2)
