title: How to prompt for exit when back button is pressed?
body: <p>Recently I had to add a feature to a <strong>Xamarin Forms</strong> mobile application. The feature shows a yes/no prompt when the user presses the back button (in Android phones). If the user selects <em>No</em> it will discard the button, otherwise, it will apply the back button which will hide the application.</p>
  
  <p>I knew that for <strong>detecting</strong> the back button in <strong>Xamarin Forms</strong> I have to override the <strong>Page.OnBackButtonPressed</strong> method. And for bypassing the back button it should return <strong>true</strong>, like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>protected override bool OnBackButtonPressed()
  {
      if (DisplayAlert("", "Are you sure?", "Yes", "No"))
          return false;
  
      return true;
  }
  </code></pre>
  
  <p>But the problem lies in the fact that <strong>Page.DisplayAlert</strong> is an async method and must be called on the main (UI) thread. So after a lot of searches, I came up with this idea which I want to share.</p>
  
  <p>I'm open to any ideas/suggestions on how to improve.</p>