How to terminate a Xamarin application?

80,618

Solution 1

If you are using Xamarin.Forms create a Dependency Service.

Interface

public interface ICloseApplication
{
    void closeApplication();
}

Android : Using FinishAffinity() won't restart your activity. It will simply close the application.

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        var activity = (Activity)Forms.Context;
        activity.FinishAffinity();
    }
}

IOS : As already suggested above.

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        Thread.CurrentThread.Abort();
    }
}

UWP

public class CloseApplication : ICloseApplication
{
    public void closeApplication()
    {
        Application.Current.Exit();
    }
}

Usage in Xamarin Forms

var closer = DependencyService.Get<ICloseApplication>();
    closer?.closeApplication();

Solution 2

A simple way to make it work cross platform is by this command:

System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();

Got it from this link.

EDIT: After using it for a while, I discovered that .CloseMainWindow() don't kill the application, only Closes it (well, thats obvious). If you want to terminate the app (kill), you shoud use the following:

System.Diagnostics.Process.GetCurrentProcess().Kill();

Solution 3

For Android, you can do

Android.OS.Process.KillProcess(Android.OS.Process.MyPid());

iOS explicitly does not provide any API for existing an App. Only the OS can close an App.

Solution 4

For iOS, you can use this code:

Thread.CurrentThread.Abort();

For Android, as @Jason mentioned here:

Android.OS.Process.KillProcess(Android.OS.Process.MyPid());

Solution 5

System.Environment.Exit(0);

Works for me.

Share:
80,618
Nik
Author by

Nik

Updated on July 05, 2022

Comments

  • Nik
    Nik almost 2 years

    How to terminate a Xamarin application from any of the activities?

    I have tried both System.Environment.Exit(0) and System.Environment.Exit(1) as well as Finish() and killing all the activities.

    It still opens one blank page with default activity name and a black screen.

    Is there any specific solution for this?

  • Nik
    Nik over 9 years
    I had apply this code but this also returns me the same black page with title of the main activity....Is there any other solution or am i missing anything over there?
  • Nik
    Nik over 9 years
    I had apply this code but this also returns me the same black page with title of the main activity....Is there any other solution or am i missing anything over there?
  • IdoT
    IdoT over 9 years
    Well, it works for my app. Maybe you're not calling it from the main UI thread? try to terminate the app inside this lambda expression: Xamarin.Forms.Device.BeginInvokeOnMainThread( () => {Android.OS.Process.KillProcess(Android.OS.Process.MyPid());‌​}); Let me know if it helps.
  • Nik
    Nik about 9 years
    Thanks for your response.And yes i want to terminate the app from child thread,not the main UI thread and my project is in Xamarin.Android not in Xamarin.Forms so can you give me the solution for that? Thanks once again
  • IdoT
    IdoT about 9 years
    You have to be on the main thread in order to close the app. try this code: Acitivty.RunOnUiThread(() => { Android.OS.Process.KillProcess(Android.OS.Process.MyPid()); });
  • Nik
    Nik about 9 years
    Ok..I am able to achieve this using int pid = Android.OS.Process.MyPid();Android.OS.Process.KillProcess(pi‌​d); but it still shows run my application in background..is there any specific code for that?
  • IdoT
    IdoT about 9 years
    Do you want to remove your app from the background running apps? It's not a standard behavior for the user.
  • Damian
    Damian over 7 years
    Thank you! Reading on other threads they said, "can't do this in iOS". It's good to see there IS a way.
  • Durai Amuthan.H
    Durai Amuthan.H over 7 years
    @Damian - I guess apple won't allow this as its against the convention and in the appstore based iOS native apps apple monitors that the developers are not using any private api to close the app though and I am not sure have you checked if it generates any crash log just a thought
  • Litera
    Litera about 7 years
    This worked for Android for me but I had to add [assembly: Dependency(typeof(CloseApplication))] to the CloseApplication class for it to get the dependency service
  • Matt
    Matt about 6 years
    I also had to add the dependency. Here's the syntax for anyone else like me who had to think about it for a bit: [assembly: Dependency(typeof(CloseApplication))] namespace MyNamespace.Droid { class CloseApplication : ICloseApplication { public void closeApplication() { var activity = (Activity)Forms.Context; activity.FinishAffinity(); } } }
  • Karen Cate
    Karen Cate about 6 years
    I don't think this actually closes the app. It makes the window go away, but the app is still running... at least the debugger doesn't exit when run in a simulator.
  • Andy Stannard
    Andy Stannard about 6 years
    Just a thought but as Apple tells you not to do this there is a good chance an app using this technique won't get through the apple app store review.
  • jase
    jase about 6 years
    Use this if you want terminate all the application processes. (Timers, services, etch...)
  • Nick Kovalsky
    Nick Kovalsky over 5 years
    All instanced classes also remain in memory... And when you relaunch their instances are already here full of previous data.
  • Zam
    Zam over 5 years
    Works with VS2017! All other answers doesn't
  • Jason
    Jason over 5 years
    @Zam this may not be valid for newer versions of Android, but it has nothing to do with the version of the IDE that you use
  • this.myself
    this.myself almost 5 years
    Is this accepted by Apple when publishing in the App Store? For Google Play Store I don't doubt...
  • vhoyer
    vhoyer over 4 years
    I honestly don't see much difference between a Thread.CurrentThread.Abort(); and a System.Diagnostics.Process.GetCurrentProcess().Kill();
  • Quarkly
    Quarkly over 4 years
    It's a bad idea to use any Diagnostic method for production code. Use System.Environment.Exit(0) instead.
  • Matas Vaitkevicius
    Matas Vaitkevicius almost 3 years
    non universal, fails on Android.
  • Matas Vaitkevicius
    Matas Vaitkevicius almost 3 years
    Confirmed, works on both Android and iOS
  • Matas Vaitkevicius
    Matas Vaitkevicius almost 3 years
    @Quarkly System.Environment.Exit(0) Does not work on Android.
  • ToolmakerSteve
    ToolmakerSteve about 2 years
    Not necessarily. (Or more accurately, it depends on Android API level, and vendor's implemention.) I can prove this on my device by POWERING OFF. When power back on, the list shows all RECENTLY USED apps. Even though NO apps are running. (I have disabled or deleted any apps that use background services.) Proving that, at least on this device, that is NOT a list of "Open Apps" - it is a list of "Recently Used Apps" (minus any that have been manually swiped away). "Close all" does clear that list. Where the list would be, is text saying "No recently used apps.". (emphasis added).