# Ionic Hardware backbutton

**URL:** <https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544>\
**Category:** ionic-v1\
**Created:** [January 12, 2015, 5:39pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544 "2015-01-12T17:39:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 12, 2015, 5:39pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/1 "2015-01-12T17:39:36Z")

</div>

i created an app in ionic and while i was testing it i noticed that when i press the hardware back button the app completely terminates , i tried to override the event but nothing was happening , the strange about all of that is when i remove the ng-app=“starter” from my app the back button work completely fine and i can easily override it but certainly my ui becomes a mess , my question is how can i override the backbutton event ?

[code is available here](https://github.com/fadysamir/zabatni)

---

<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:** [January 12, 2015, 6:18pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/2 "2015-01-12T18:18:56Z")

</div>

So you can register a new back button action with the follow code below.

```auto
 $ionicPlatform.registerBackButtonAction(function (event) {
    // Custom function
  }, 100);

```

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 2:50pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/3 "2015-01-13T14:50:30Z")

</div>

it is not working i tried it and nothing changes

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 13, 2015, 2:57pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/4 "2015-01-13T14:57:45Z")

</div>

maybe you can show us your code?

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 3:09pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/5 "2015-01-13T15:09:28Z")

</div>

this is a link of a github repo that contain all of the code [https://github.com/fadysamir/zabatni](https://github.com/fadysamir/zabatni)

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 13, 2015, 3:13pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/6 "2015-01-13T15:13:49Z")

</div>

> This repository is empty.

> Care to check out the GitHub Channel on YouTube while you wait?

☹

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 3:15pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/7 "2015-01-13T15:15:08Z")

</div>

sorry my internet speed is too low the code is being uploaded

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 3:16pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/8 "2015-01-13T15:16:04Z")

</div>

it is now live  
, you can check it

---

<div class="post-metadata">

**Author:** ![borisIzstop](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@borisIzstop](https://forum.ionicframework.com/u/borisIzstop)\
**Post date:** [January 13, 2015, 3:21pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/9 "2015-01-13T15:21:34Z")

</div>

Well, put some custom code in it.  
Example:

```
$ionicPlatform.registerBackButtonAction(function (event) {
    // Custom function
   event.preventDefault(); // Back button will DO NOTHING. 
  $state.go(url); //go to specific url
  }, 100);
```

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 3:23pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/10 "2015-01-13T15:23:16Z")

</div>

i put an alert and tried to make it navigate to another page and nothing was happening. @mhartington

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 13, 2015, 4:44pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/11 "2015-01-13T16:44:42Z")

</div>

i can only find this line in your index.html

```
<script type="text/javascript">document.addEventListener("backbutton", onBackKeyDown, false);

```

You have to create a controller and there you need to load $ionicPlatform and then use the suggested code by mhartington.

I am using this approach in all my apps and it is working.

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 5:00pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/12 "2015-01-13T17:00:06Z")

</div>

check the test page i typed it there

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [January 13, 2015, 5:04pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/13 "2015-01-13T17:04:55Z")

</div>

Yeah and i told you… you need to create a angularjs controller for your module and put that code in there…

maybe you have to read some angularjs tutorials.

[https://docs.angularjs.org/guide/controller](https://docs.angularjs.org/guide/controller)

If you want to build ionic apps you have to learn angularjs.

---

<div class="post-metadata">

**Author:** ![fady](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/fady/32/8389_2.png) [@fady](https://forum.ionicframework.com/u/fady)\
**Post date:** [January 13, 2015, 5:23pm UTC](https://forum.ionicframework.com/t/ionic-hardware-backbutton/15544/14 "2015-01-13T17:23:08Z")

</div>

i think may be this is my problem i do not know anything about angular js and it seems that all ionic functions are based on top of it
