Execute a function to interact with user and then pop a page

I’m here in the docs trying to find a way to do the following:

The user’ll be on a form page, and when he goes back i need to do a verification like “are you sure you want to discard you alterations?” and when he click yes or no it’ll execute a function and then leave the page.

I know i can use a Platform.registerBackButtonAction() to check when he clicks the hardware back button, but how can i do the same for the NavControll back button on the header?

I can use NavGuards and it’s automatically implemented on the NavControll back button, but it leaves the page and THEN executes the function.

So the flow of what i need to do is:

enter page >> write something on any input >> if it tries to leave the page open the check alert >> on clicking ‘yes’ it leaves the page

Here is a piece of code of what i’m doing:

ionViewCanLeave() {
  let a = this.alerts.create({
    title: "Confirmation message?",
    buttons: [{
        text: 'Nop',
        handler: () => {
            this.navCtrl.pop();
        }
    }, {
        text: 'Yes',
        handler: () => {
            this.salvarDescricao();
        }
    }]
  });

  if (this.changes != undefined && this.changes!= '') { //just a check i do, if the user doesn't change anything i don't need to ask
    a.present();
  } else {
    this.navCtrl.pop(); //if nothing changes it pops twice
  }
}

So how can i achieve this? How to prevent the user from leaving the view, executing a code and depending on the results it leaves?

Thanks :slight_smile: