# Ionic 4: Prevent backbutton from closing page

**URL:** <https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988>\
**Category:** Ionic Framework\
**Created:** [September 27, 2018, 12:39pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988 "2018-09-27T12:39:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![highwaychile](https://avatars.discourse-cdn.com/v4/letter/h/9de053/32.png) [@highwaychile](https://forum.ionicframework.com/u/highwaychile)\
**Post date:** [September 27, 2018, 12:39pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/1 "2018-09-27T12:39:19Z")

</div>

Hi,

I’m searching for a way to disable the hardware back button on certain pages, i.e. on a “loading” page; I know about the existence of

```auto
this.platform.backButton.subscribe(() => {
  // do something here
});

```

, but this allows only for performing an action once the back button is pressed, but I don’t see how to cancel the “nav.pop()” action. Do you know how to achieve this?

---

<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 30, 2018, 6:58pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/2 "2018-09-30T18:58:48Z")

</div>

I just realize and face the exact same problem, did you find any solution?

---

<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:** [October 1, 2018, 5:42am UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/3 "2018-10-01T05:42:59Z")

</div>

I don’t think it’s possible, I have opened a feature request: [https://github.com/ionic-team/ionic/issues/15820](https://github.com/ionic-team/ionic/issues/15820)

---

<div class="post-metadata">

**Author:** ![mbusby](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mbusby/32/121336_2.png) [@mbusby](https://forum.ionicframework.com/u/mbusby)\
**Post date:** [October 1, 2018, 5:56am UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/4 "2018-10-01T05:56:39Z")

</div>

I think what you would want to do here is use the lifecycle hook `ionViewCanLeave()` inside your loading page to determine if you can leave the view. More lifecycle hooks can be found here: [https://ionicframework.com/docs/api/navigation/NavController/#lifecycle-events](https://ionicframework.com/docs/api/navigation/NavController/#lifecycle-events)

---

<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:** [October 1, 2018, 7:25am UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/6 "2018-10-01T07:25:57Z")

</div>

Oh you are suggesting that was a workaround, I should in addition to subscribing to the back button, I should implement an `ionViewCanLeave()` in order to avoid the automatic back navigation?

That’s a good idea 👍 I should try…

---

<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:** [October 1, 2018, 8:15am UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/7 "2018-10-01T08:15:03Z")

</div>

Except that there isn’t any `ionViewCanLeave` in Ionic v4 right?

---

<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:** [October 1, 2018, 12:07pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/8 "2018-10-01T12:07:34Z")

</div>

Ok so I could confirm what I said above, there isn’t a `ionViewCanLeave` in v4, therefore you could not add a rule to tells the back action to be or not to be executed, but feel free to correct me if that’s wrong, that would be cool

In the meantime I may I have found, at least for my app, an ugly workaround. I want to override the back action because in my page there is a slider, so when the user click back, I don’t want to navigate back but I want to navigate to previous slide. The to so I subscribe to the `platform.backButton` in order to call `slider.slidePrev()`

Then I have to solve that problem of auto navigation, so the trick which seems to work, is to add state in the history, therefor, the automatic back action (see `goBack` or the `ion-router` [https://github.com/ionic-team/ionic/blob/7d50305b7f6c48392dd278b55824e4c712890214/core/src/components/router/router.tsx](https://github.com/ionic-team/ionic/blob/7d50305b7f6c48392dd278b55824e4c712890214/core/src/components/router/router.tsx)) will go back in the navigation history by first popping the states I added

Looks like

```
<ion-slides (ionSlideNextEnd)="pushHistoryState()">

async pushHistoryState() {
        const sliderIndex: number = await this.slider.getActiveIndex();
        history.pushState({newAd: sliderIndex}, document.title);
    }
}

```

also of course you should do the same for the action which would slide to previous slide by poping the state

```
 history.back(1);
```

---

<div class="post-metadata">

**Author:** ![HinderiksR](https://avatars.discourse-cdn.com/v4/letter/h/9dc877/32.png) [@HinderiksR](https://forum.ionicframework.com/u/HinderiksR)\
**Post date:** [October 1, 2018, 12:19pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/9 "2018-10-01T12:19:12Z")

</div>

Would love to see a “real” non hacky solution for this. I ran into this issue 5 minutes ago trying to show a pop up going like “Are you sure you dont want to save, bro ?”. Thought i could override it by putting a (tap) on the ion-back-button but no sigar

---

<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:** [October 1, 2018, 2:50pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/10 "2018-10-01T14:50:38Z")

</div>

I would like to have a real non hacky solution and I like the idea you posted in the GitHub issue 👍

In the meantime, I could confirm that the above ugly hack works fine. Implemented and tested on my Android phone

---

<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:** [October 2, 2018, 11:42am UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/11 "2018-10-02T11:42:16Z")

</div>

There will be a new feature for that purpose in the next Ionic v4 version, @manucorporat implemented a new feature which will be called `subscribeWithPriority` (see code [https://github.com/ionic-team/ionic/commit/6a5aec8b5d76280ced5e8bb8fd9ea6fe75fe6795](https://github.com/ionic-team/ionic/commit/6a5aec8b5d76280ced5e8bb8fd9ea6fe75fe6795)) 👍

so to follow OP first post:

```
this.platform.backButton.subscribeWithPriority(() => {
    // do something here
});

```

cool

---

<div class="post-metadata">

**Author:** ![mmubasher](https://avatars.discourse-cdn.com/v4/letter/m/2bfe46/32.png) [@mmubasher](https://forum.ionicframework.com/u/mmubasher)\
**Post date:** [November 28, 2018, 2:11pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/12 "2018-11-28T14:11:55Z")

</div>

I wanted to prevent back button event from propagation. To do this I will update your snippet as

```auto
this.platform.backButton.subscribeWithPriority(() => {
    // do nothing here
});

```

---

<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:** [July 19, 2021, 9:15pm UTC](https://forum.ionicframework.com/t/ionic-4-prevent-backbutton-from-closing-page/142988/13 "2021-07-19T21:15:58Z")

</div>

Closed on July 19, 2021, 9:15pm UTC


