How To Confirm Exit in Android with Xamarin Forms

Update – 2020-03-05

I updated the samples and snippets to include some reader feedback:

  1. Accounted for Master-Detail navigation in App.PromptToConfirmExit
  2. Provided a toast + back button confirmation alternative sample
  3. Updated sample: allow toggling between Master-Detail and Tabbed navigation; allow toggling between dialog and toast confirmation styles.


Ever hit the navigation bar back button when using an Android app and have it fully exit the app and drop you back on your home screen? I think all Android users have encountered this a few times. It is a frustrating and counter-productive experience. Let’s make sure your app has better UX by using a simple confirmation dialog!

Xamarin Forms Android Exit Confirmation

Good news, this is easy to implement and should only take you a few minutes to code and test.

Step 1

Create a property to check if there are pages on your navigation stack. Here’s my implementation:

Step 2

Override the OnBackPressed method in your app’s MainActivity to intercept back navigation. Here’s a sample:

We’re Done!

Wasn’t that easy! Let me know how this works out for your app. Check out my full Xamarin Forms Android Exit Confirmation sample on Github.

7 Replies to “How To Confirm Exit in Android with Xamarin Forms”

  1. You should use the “back again to exit” pattern with a toast, that is used by most apps. A dialog is annoying for most users in my opinion.

    private bool _isBackPressed;

    public override void OnBackPressed()
    _isBackPressed = true;
    Android.Widget.Toast.MakeText(this, “Press back again to close”, Android.Widget.ToastLength.Short).Show();

    // Disable back to exit after 2 seconds.
    new Handler().PostDelayed(() =>
    _isBackPressed = false;
    }, 2000);

  2. I remember reading that the confirmation dialog shouldn’t have “Are you sure…?” unless they are about to do something dangerous like delete something. People are likely to hit “No” as they aren’t really sure and are surprised the application didn’t exit. “Exit?” is fine.

    Also “Confirm Exit” is a statement and not a question.

  3. My iteration on Mark’s code:

    private bool _isConfirmExitToastVisible;

    public override void OnBackPressed()
    var rootView = App.Current.MainPage.Navigation.NavigationStack.Last() as RootView;
    if (rootView != null)
    //rootView is the ‘root page’, pressing back would indicate a users intent of closing the app
    if (_isConfirmExitToastVisible)
    .MakeText(this, “Tap to close”, Android.Widget.ToastLength.Short).Show();
    _isConfirmExitToastVisible = true;

    // Disable back to exit after 2 seconds.
    new Handler().PostDelayed(() => { _isConfirmExitToastVisible = false; }, 2000);

  4. Hi there, I am using prism and copied and pasted your code and works, however I am not sure how it should work when you have a modal page, I was expecting to prompt for the exit only when you do not have a modal page.
    When you have launched a modal form and press back button it should NOT prompt for the exit but just going back.

    Am I missing the obvious?

    1. public bool PromptToExit
      INavigation [] allNavigations = GetAllNavigations ( this.MainPage );
      return allNavigations.Aggregate ( 0 , ( total , next ) => next.ModalStack.Count + next.NavigationStack.Count )

Leave a Reply to Sjors Miltenburg Cancel reply

Your email address will not be published. Required fields are marked *