Skip to content

Instantly share code, notes, and snippets.

@Stayrony
Created July 20, 2018 21:30
Show Gist options
  • Select an option

  • Save Stayrony/db39613bdaf7ac49c382395fba21228a to your computer and use it in GitHub Desktop.

Select an option

Save Stayrony/db39613bdaf7ac49c382395fba21228a to your computer and use it in GitHub Desktop.
How to Exit App When Press Back Button - Xamarin.Android
...
public override void OnBackPressed()
{
RunOnUiThread(
async () =>
{
var isCloseApp = await AlertAsync(this, "NameOfApp", "Do you want to close this app?", "Yes", "No");
if (isCloseApp)
{
var activity = (Activity)Forms.Context;
activity.FinishAffinity();
}
});
}
public Task<bool> AlertAsync(Context context, string title, string message, string positiveButton, string negativeButton)
{
var tcs = new TaskCompletionSource<bool>();
using (var db = new AlertDialog.Builder(context))
{
db.SetTitle(title);
db.SetMessage(message);
db.SetPositiveButton(positiveButton, (sender, args) => { tcs.TrySetResult(true); });
db.SetNegativeButton(negativeButton, (sender, args) => { tcs.TrySetResult(false); });
db.Show();
}
return tcs.Task;
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment