Skip to content

Instantly share code, notes, and snippets.

@dylanberry
Last active March 5, 2020 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanberry/1a4f906a8c1948f7bbf8487d6ee015c0 to your computer and use it in GitHub Desktop.
Save dylanberry/1a4f906a8c1948f7bbf8487d6ee015c0 to your computer and use it in GitHub Desktop.
// Confirm with dialog
public override void OnBackPressed()
{
if (((ConfirmBack.App)App.Current).PromptToConfirmExit)
{
using (var alert = new AlertDialog.Builder(this))
{
alert.SetTitle("Confirm Exit");
alert.SetMessage("Are you sure you want to exit?");
alert.SetPositiveButton("Yes", (sender, args) => { FinishAffinity(); }); // inform Android that we are done with the activity
alert.SetNegativeButton("No", (sender, args) => {}); // do nothing
var dialog = alert.Create();
dialog.Show();
}
return;
}
base.OnBackPressed();
}
// Confirm with toast + back button
bool _isBackPressed = false;
public override void OnBackPressed()
{
var app = (ConfirmExitMasterDetail.App)App.Current;
if (app.PromptToConfirmExit)
{
if (_isBackPressed)
{
FinishAffinity(); // inform Android that we are done with the activity
return;
}
_isBackPressed = true;
Toast.MakeText(this, "Press back again to exit", ToastLength.Short).Show();
// Disable back to exit after 2 seconds.
new Handler().PostDelayed(() => { _isBackPressed = false; }, 2000);
return;
}
base.OnBackPressed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment