How to close an App on Xamarin

Paolo Mossa 181 Reputation points
2022-01-20T11:54:12.317+00:00

Hi guys. I am developing an App with Xamarin on Android. For closing the App I wrote the following code:

Application.Current.Quit();

But does not work. The program continues to run...

I ask a suggestion

Thanks in advanced for any reply

Developer technologies | .NET | Xamarin
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2022-01-21T01:23:25.037+00:00

    Hello PaoloMossa-0994,​

    Welcome to our Microsoft Q&A platform!

    I am developing an App with Xamarin on Android.

    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.

       [assembly:Dependency(typeof(DroidImplementation))]  
       namespace TestApplication1.Droid  
       {  
           public class DroidImplementation : MyInterface  
           {  
               public void QuitApplication()  
               {  
                   var activity = MainActivity.Instance as Activity;  
                   activity.Finish();  
               }  
           }  
       }  
         
       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
       {  
           public static MainActivity Instance { get; set; }  
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
               Instance = this;  
               ...  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,196 Reputation points
    2022-01-20T11:59:41.637+00:00

    Maybe Application.Quit()

    xamarin.forms.application.quit


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.