How to exit the App in Xamarin Forms?

13,173

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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.

Share:
13,173
Senthamizh
Author by

Senthamizh

Updated on June 05, 2022

Comments

  • Senthamizh
    Senthamizh almost 2 years

    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.

  • Senthamizh
    Senthamizh over 7 years
    It worked like charm sumeet... Thanks a lot man... But I'm still struggling to get a way to exit app in Xamarin.Windows. I'm trying from my side and your help/guidance will be appreciated...
  • Senthamizh
    Senthamizh over 7 years
    Your code helped me to make my project even better. But I want to clear the navigation stack when particular page is executed, say when the user clicks Resources page I want to clear the navigationstack history, Is there any way...?? And by the way thanks for your time...
  • Dima
    Dima over 7 years
    You can push Resources page to navigation.InsertPageBefore () navigation.PopToRootAsync(); but it's not a good idea.What I did is:
  • Dima
    Dima over 7 years
    ' for (int i = 0; navigation.NavigationStack.Count > 1;) {// dispose all pages var page = navigation.NavigationStack.ElementAt(i); if (page.BindingContext != null && page.BindingContext is BaseViewModel) { (page.BindingContext as BaseViewModel).OnDisappearing(); page.BindingContext = null; } navigation.PopAsync(false);//not animated navigation.RemovePage(page);//crash with AnimationNavigationPage }'
  • Dima
    Dima over 7 years
    Sorry, pressed enter before edited the code. You can do: 'navigation.InsertPageBefore()' for the current page and call 'navigation.PopAsync(false)' as number of pages -1. You should do it before navigating to Resource page.
  • Senthamizh
    Senthamizh over 7 years
    Great... I used your idea to my code which fortunately worked out in a better way... Thanks a lot again...