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?
When building a Xamarin. Forms project on Android platform, all pages are shown on the MainActivity. To close the app, you could call the Finish method of MainActivity using DependencyService. If the response is helpful, please click "Accept Answer" and kindly upvote it.
Exit(0) and System. Environment. Exit(1) as well as Finish() and killing all the activities.
You can't explicitly restart an App - iOS specifically prohibits this, and there is no universal mechanism to do this on other platforms. If you determine that the session has expired, you will need to prompt the user to login, and do any initialization manually.
The only way to delete the Project and Android subfolder is to reboot my computer.
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();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With