Ionic 4 | Overridden Back Press Event and Show Exit Confirm on Application Close

We have already discussed How to Override Back Press Event in Ionic’s V3 which we can easily do by using Platform’s registerBackButtonAction method. But in Ionic’s V4 version this method simply does not work.

So in this post, we will create an Ionic V4 Application to demonstrate overriding of Back Press Event in Ionic 4 Application. We will also discuss how to get the current Component name to handle it more conveniently.

Let’s create an Ionic 4 Application first using following CLI command as Ionic 4 is still in beta phase.

In Ionic 4 Application we use Platform’s backButton Interface as follows

Here 999990 is the priority, we need to give this number due to some ongoing bugs in library 😛

Implement Back Press Event in any specific Component

Open home.page.ts file then replace the following code

Here priority is 999999 which should be greater then app.component.ts event priority.

In ionViewWillLeave event will unsubscribe BackButton event when app moves to any other view/ page.

 

Leave a Reply

avatar
  Subscribe  
Notify of