# Overriding the back button handler

**URL:** <https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511>\
**Category:** ionic-v1\
**Created:** [September 30, 2015, 3:21am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511 "2015-09-30T03:21:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daddicool](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/daddicool/32/90973_2.png) [@daddicool](https://forum.ionicframework.com/u/daddicool)\
**Post date:** [September 30, 2015, 3:21am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/1 "2015-09-30T03:21:29Z")

</div>

I have the following code in index.html:

```
<body ng-app="starter">
<!-- The nav bar that will be updated as we navigate -->
  <ion-nav-bar>
  <ion-nav-back-button class="button-dark"/>
  </ion-nav-bar>

  <!-- where the initial view template will be rendered -->
  <ion-nav-view> <ion-view>
     <ion-content>Hello!</ion-content>
  </ion-view> </ion-nav-view>

```

Because of this a back button appears on every view. On one of the view, I want to override the back button logic i.e I want to prompt the user asking whether he wants to really go back. How do I achieve this?

Thanks.

---

<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:** [September 30, 2015, 5:49am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/2 "2015-09-30T05:49:52Z")

</div>

[http://ionicframework.com/docs/api/service/$ionicPlatform/](http://ionicframework.com/docs/api/service/%24ionicPlatform/)

there is a registerBackButtonAction-Function to achieve this.

In the controller to that view --\> register a backbuttonaction with priority 100.  
use event.preventDefault do prevent default back-action.

Show popup --\> if user wants to go back --\> call $ionicHistory.back(); and remove registered BackButtonAction!

---

<div class="post-metadata">

**Author:** ![daddicool](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/daddicool/32/90973_2.png) [@daddicool](https://forum.ionicframework.com/u/daddicool)\
**Post date:** [September 30, 2015, 6:27am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/3 "2015-09-30T06:27:07Z")

</div>

The documentation says it is for the “hardware” back button. Not my back button in the header.

---

<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:** [September 30, 2015, 6:52am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/4 "2015-09-30T06:52:50Z")

</div>

oh okay sry, then i misunderstood you.

you can add the ionNavBackButton an own ngClick function --\> there you can check the current state --\> if it is not the special handling --\> show Popup and in all other cases call $ionicHistory.goBack()

---

<div class="post-metadata">

**Author:** ![daddicool](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/daddicool/32/90973_2.png) [@daddicool](https://forum.ionicframework.com/u/daddicool)\
**Post date:** [September 30, 2015, 9:25am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/5 "2015-09-30T09:25:10Z")

</div>

How do I know in the function if I am on the ‘special’ page?

Should I put the ngclick handler in $rootScope?

---

<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:** [September 30, 2015, 11:35am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/6 "2015-09-30T11:35:42Z")

</div>

You need a base controller that wraps the whole app-content  
there you can define the click-action.

The uiRouter-Module (included in ionic) provides the $state-Service.  
With that you can easily get the current state name.

---

<div class="post-metadata">

**Author:** ![daddicool](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/daddicool/32/90973_2.png) [@daddicool](https://forum.ionicframework.com/u/daddicool)\
**Post date:** [September 30, 2015, 4:42pm UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/7 "2015-09-30T16:42:21Z")

</div>

Tried it doesn’t work. The suggestions are too vague for a beginner like me. The problem I am facing is a simple problem and ionic seems to not have solutions for simple problems it seems. I have seen similar posts by others an there are no solutions.

Thanks anyway.

---

<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:** [October 1, 2015, 5:43am UTC](https://forum.ionicframework.com/t/overriding-the-back-button-handler/33511/8 "2015-10-01T05:43:51Z")

</div>

the problem is… that the solutions provided by ionic are simple… if you are in angular before.

So if you want to write an app with angularjs and without no javacript know how… there are no simple solutions.  
There are no simple solutions in all programming languages in that case^^.

If you want to build a software like with good old delphi with drag an drop, everything is colorful and clickable and you do not have to write a single line of code … maybe you should not program anything.

Every technology has there specials and tricks you have to dig into it and learn to work with them…

If you have no experiences maybe you should first build a little angularjs website and then make it work with ionic…  
without special usecases like overwriting backbuttons and so on.

Simple and clean. Define a Baselayout with navbar and backbutton and you content.  
Understand how ionic works.
