Xamarin Forms Software Back Button Event Capture

Dinesh Falwadiya
2 min readMar 2, 2021

--

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

  1. MainPage
  2. 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

Main Page

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dinesh Falwadiya
Dinesh Falwadiya

Written by Dinesh Falwadiya

Mobile app developer who loves to share his knowledge and get improve with others.

No responses yet

Write a response