Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate a Xamarin application?

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?

like image 615
Nik Avatar asked Mar 25 '15 14:03

Nik


People also ask

How do I exit an application in xamarin?

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.

How do I close apps on Android xamarin?

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

How do I restart an app in xamarin?

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.

Can I delete xamarin folder?

The only way to delete the Project and Android subfolder is to reboot my computer.


2 Answers

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(); 
like image 54
Akash Amin Avatar answered Sep 18 '22 04:09

Akash Amin


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(); 
like image 43
vhoyer Avatar answered Sep 19 '22 04:09

vhoyer