How to terminate a Xamarin application?
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.
Nik
Updated on July 05, 2022Comments
-
Nik almost 2 years
How to terminate a Xamarin application from any of the activities?
I have tried both
System.Environment.Exit(0)
andSystem.Environment.Exit(1)
as well asFinish()
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 over 9 yearsI 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 over 9 yearsI 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 over 9 yearsWell, 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 about 9 yearsThanks 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 about 9 yearsYou 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 about 9 yearsOk..I am able to achieve this using int pid = Android.OS.Process.MyPid();Android.OS.Process.KillProcess(pid); but it still shows run my application in background..is there any specific code for that?
-
IdoT about 9 yearsDo you want to remove your app from the background running apps? It's not a standard behavior for the user.
-
Damian over 7 yearsThank 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 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 about 7 yearsThis 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 about 6 yearsI 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 about 6 yearsI 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 about 6 yearsJust 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 about 6 yearsUse this if you want terminate all the application processes. (Timers, services, etch...)
-
Nick Kovalsky over 5 yearsAll instanced classes also remain in memory... And when you relaunch their instances are already here full of previous data.
-
Zam over 5 yearsWorks with VS2017! All other answers doesn't
-
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 almost 5 yearsIs this accepted by Apple when publishing in the App Store? For Google Play Store I don't doubt...
-
vhoyer over 4 yearsI honestly don't see much difference between a
Thread.CurrentThread.Abort();
and aSystem.Diagnostics.Process.GetCurrentProcess().Kill();
-
Quarkly over 4 yearsIt's a bad idea to use any Diagnostic method for production code. Use System.Environment.Exit(0) instead.
-
Matas Vaitkevicius almost 3 yearsnon universal, fails on Android.
-
Matas Vaitkevicius almost 3 yearsConfirmed, works on both Android and iOS
-
Matas Vaitkevicius almost 3 years@Quarkly
System.Environment.Exit(0)
Does not work on Android. -
ToolmakerSteve about 2 yearsNot 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).