8

How can I intercept the back button events in xamarin forms for Android and IOS and be able to show the user an alert to confirm the exit?

The objective is to intercept the 2 buttons, the navigation menu (yellow) and the device (orange)

I have seen some examples, but they are several years old.

I am using VS 2019 and the latest version of xamarin forms.

enter image description here

Thank you in advance for your help.

Liam
  • 27,717
  • 28
  • 128
  • 190

1 Answers1

1

You can Use Title View instead of default navigation bar and handle the back button event. Please refer: https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/TitleView

also you can handle device back button event by override it like this:

protected override bool OnBackButtonPressed()
{
   //return true to prevent back, return false to just do something before going back. 
  return true;
}

Hope this may resolve your issue.

MShah
  • 1,247
  • 9
  • 14