Last active
March 5, 2020 19:45
-
-
Save dylanberry/1a4f906a8c1948f7bbf8487d6ee015c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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