简体   繁体   中英

How to exit the App in Xamarin Forms?

My project is built with master-detail navigation. There are totally three pages in the list named as Resources, Contacts, and Login. Everything works fine in iOS, but when the user presses the Droid/WinPhone devices hardware back button, the app should exit. Is there any app-exit mechanism for Xamarin Forms which will work on all the devices.? (I mean native code not platform dependent) Thanks in advance.

I did that on this way

In xamarin forms I added interface

public interface INativeHelper
{
    void CloseApp();
}

In android project I made implementation of INativeHelper

public class NativeHelper : INativeHelper
{
    public void CloseApp()
    {
        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
    }                
}

Implementation of INativeHelper in IOS

public class NativeHelper : INativeHelper
{
    public void CloseApp()
    {
        Process.GetCurrentProcess().CloseMainWindow();
        Process.GetCurrentProcess().Close();
    }       
}

And then just override method OnBackButtonPressed in page in Xamarin.Forms project

protected override bool OnBackButtonPressed()
    {
        INativeHelper nativeHelper = null;
        nativeHelper = DependencyService.Get<INativeHelper>();
        if (nativeHelper != null)
        {
            nativeHelper.CloseApp();
        }  

        return base.OnBackButtonPressed();
    }

I didn't made implementation for WinPhone, but it should be similar.

You can use a DepedencyService for closing an app when your physical back button is pressed:

In your UI (PCL), do the following:

protected override bool OnBackButtonPressed()
{
   if (Device.OS == TargetPlatform.Android)
       DependencyService.Get<IAndroidMethods>().CloseApp();

   return base.OnBackButtonPressed();
}

Now implement the 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());
       }
   }
}

Also create an Interface (in your UI PCL):

public interface IAndroidMethods
{
    void CloseApp();
}

As far as I know there is no native way to exit the app in Xamarin application.

The only way is to use dependency service. Override OnBackButtonPressed function in your ContentPage and check it is the last page:

protected override bool OnBackButtonPressed()
{
 if(navigation.NavigationStack.Count == 1)//navigation is MainPage.Navigation
  DependencyService.Get<YourDependencyInterface>().CloseApp();
}

For Android in YourAndroidDependency class:

public void CloseApp()
{
 (Xamarin.Forms.Forms.Context as Activity).Finish();
}

As for WinPhone I'm not sure but I believe it can be done in same way - dependency service.

Having experimented with all the above, I found that none of the above worked on a Google Pixel 3a, with latest version of Android

The command that came closest was

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

However it left the remains of the app still visible in the background.

The following worked for me when called from the Android Main Activity

    public void ExitApp()
    {
        this.FinishAndRemoveTask();
        Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
    }

The first line FinishAndRemoveTask removes the app from both the foreground and the background, however the Application process is still active, hence the need for the second command.

This is the more easy way found:

public void QuitApp(object sender, EventArgs args)
{
    Process.GetCurrentProcess().CloseMainWindow();
    Process.GetCurrentProcess().Close();
}

PS: Tested in android

You can use Environment.Exit(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM