Created
July 20, 2018 21:30
-
-
Save Stayrony/db39613bdaf7ac49c382395fba21228a to your computer and use it in GitHub Desktop.
How to Exit App When Press Back Button - Xamarin.Android
This file contains hidden or 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
| ... | |
| 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