Xamarin Forms Software Back Button Event Capture
I have been developing mobile apps in xamarin forms for quite a few years now, and there is always been a requirement to show a confirmation popup on the software back button pressed in android. So here is what I found the solution from various other xamarin experts and tweaked the code a bit to meet our requirement.
I will be using Xamarin Prism Code Structure, which helps us to create maintainable & loosely coupled apps.

Our App will have 2 pages
- MainPage
- DetailPage
MainPage will show only a button to open the details page
The detail page will show the confirmation popup when pressed back from both Hardware & Software back buttons
MainPage View
MainPage ViewModel

DetailPage View
DetailPage Code Behind
Description: Handled the hardware back button press event
DetailViewModel

ViewModelBase
Note: Here the CanNavigateBack & BackPressedAction are the 2 important properties required for our implementation
Registering Views with ViewModel using a prism in App.xaml.cs
Android Implementation
Description: To capture the software back button press event I have used the NavigationPage custom renderer of xamarin forms & set the click listener to our NavigationBar.

iOS Implementation
Make sure to assign BackPressedAction in the DetailViewModel to see everything working.