Xamarin Close Android app on back button

Some time in your application, it is needed to prompt user before exit from application. In this article, we will see how you can prompt user on pressing back button with a dialog, questioning whether or not the user wishes to exit the application. So, in this article we are going to learn how to prevent user to exit from application without giving response.

Getting it

Go to your Droid project and open MainActivity.cs file and add below code to your onBackPressed() method.

AlertDialog described here.

As a result of clicks back button user will be asked for confirmation for exit:

screenshot

Written on July 21, 2018
comments powered by Disqus