Xamarin application close the Android application on the button

I tried 3 different ways

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) System.Environment.Exit(0); public override void OnBackPressed() { Finish(); } public override bool OnKeyDown(Keycode keyCode, KeyEvent e) { if (keyCode == Keycode.Back) { System.Environment.Exit(0); return true; } return base.OnKeyDown(keyCode, e); } 

None of the above work

+7
source share
7 answers

You can use DepedencyService to close the application when you press the physical back button:

In your user interface (PCL), do the following:

 protected override bool OnBackButtonPressed() { if (Device.RuntimePlatform == Device.Android) DependencyService.Get<IAndroidMethods>().CloseApp(); return base.OnBackButtonPressed(); } 

Also create an interface (in your PCL interface):

 public interface IAndroidMethods { void CloseApp(); } 

Now embed Android-specific logic in your Android project:

 [assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))] namespace Your.Namespace { public class AndroidMethods : IAndroidMethods { public void CloseApp() { Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); } } } 
+20
source

If you want to exit the application without killing and returning to the main screen, so if you want to resume it back from where it is closed. You can perform the following actions in your related activities.

  public override void OnBackPressed() { Intent startMain = new Intent(Intent.ActionMain); startMain.AddCategory(Intent.CategoryHome); startMain.SetFlags(ActivityFlags.NewTask); StartActivity(startMain); } 

Hope this helps.

+3
source

I tried this code, it works on my part. Hope this code helps you.

 protected override bool OnBackButtonPressed() { Device.BeginInvokeOnMainThread(async () => { var result = await DisplayAlert("Alert!", "Do you really want to exit the application?", "Yes", "No"); if (result) { if (Device.OS == TargetPlatform.Android) { Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); } } }); return true; } 
+1
source
 public override void OnBackPressed() { Finish(); Android.OS.Process.KillProcess (Android.OS.Process.MyPid ()); } 
0
source

Try this in your activity:

 this.FinishAffinity(); 

Good luck

0
source
 Finish(); 

Worked for me. Made from the main activity.

0
source

You can also ask the user to confirm the exit from the application:

 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; } 

Xamarin.Android are waiting for AlertDialog.Builder

0
source

All Articles