Forum Xamarin.Forms

How to close my app?

I want to close my app from click event. But i can't find a method to do so.
Is this implemented in Xamarin.Forms or do i have to make a DependencyService?
If i have to make an DependencyService can you give an advice how?

Posts

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    in iOS you cannot explicitly close your own app, so it's unlikely Forms provides a cross-platform way to do it

  • FrTerstappenFrTerstappen DEMember

    @JasonAwbrey‌ Thanks for the info. I will then just remove that option from my app.
    How can i mark this question answered?

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    Out of curiosity, why would you even want to close application? Even if you do you would just hide it, not really close it (Android way, I guess others are similar).

  • FrTerstappenFrTerstappen DEMember

    @MihaMarkic‌ I have an activation screen on which you have to enter an activation code on first launch. Positive result is a message box with an okay button and negative would be an message box with retry and close button. Now it will just be one button with dismiss.

  • davidgracondavidgracon USMember ✭✭

    I use Android.OS.Process.KillProcess (Android.OS.Process.MyPid ());
    on to kill my process when the user exits. My app is started from a captive menu and it's important that the app 'exits' so a new version can be installed if need be using MobiControl.

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    @FrTerstappen‌ Ah, I see. Or you might just show an activity that says login failed with a button "Retry".

    @davidgracon‌ Perhaps I am missing something but the app can be updated even it you don't kill its process, isn't that so? Can't MobiControl do that?

  • GadiPazGadiPaz ILUniversity

    @MihaMarkic‌
    We have a business app which the employee sends his location to the back office (automatically, the mobile app sends the location every 10 min).
    For privacy reasons, the employee can close the application if he doesn't want his manager to track him.
    When the employee press the "Exit" button I want to send "closingCommand" to the server and close the application.

    This is one of the scenarios that I would like to close the application

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    I guess the same can be done without closing the app. Just stop tracking when user hits "Exit"?

  • davidgracondavidgracon USMember ✭✭

    @MihaMarkic‌ - The issue is that if I just do a Finish in the first Activity the app is still not stopped. By Killing the Process the user can rerun the app from a captive menu cleanly. I am hooked up to a barcode scanner using a BroadcastReceiver. There were times when the scanner wouldn't work properly when the first activity did a Finish without killing self. Also during beta we would run into an occasional Unhandled Exception which is 'caught' in the first activity and by killing self the user could rerun the app. The app (12,000 lines of code) was converted from VB.NET to Xamarin Android and I had to mimic or improve previous behavior. On Windows Mobile users would do hard/soft resets when things went wrong. Now we take care of things in a way that they won't be doing that. All I can say is that it works great and the client is very, very happy with the Motorola MC40 Android remake (originally a Telxon TCAL app) of their app. Oh, and I love Xamarin.

  • fernandopfernandop USMember ✭✭

    Hi I am new in Xamarin.Forms. How do you call Android.OS.Process.MyPid () from a page in the PLC project? thanks.

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @fernandop‌ you cannot call it directly from within PCL, instead you would write an Interface and use DependencyService to access the native implementation in your Android project.

    You would also need to implement something for the other platforms (even though they may not have the same 'quit' capability) or else use if (Device.OS == TargetPlatform.Android) { before calling the 'killprocess' dependency service.

  • fernandopfernandop USMember ✭✭

    Nice, I will take a look on the link and try to implement that, thanks!

  • mansouransarimansouransari USMember
    edited March 2015

    Very confused by the Dependency Service example at the present time.

    Isn't there a simple code recipe example to KillProcess to kill the application for Android Project in Xamarin? Such as a simple form with a KillProcess button that actually kills the process would be very helpful to those of us just learning Xamarin for Android.

    Using Microsoft Visual Basic (for Windows) it is extremely easy to exit a program. For Example:

         Me.Close()
         End
    

    It seems very unnatural and very unintuitive to leave a program running when you want to cleanly exit it. As long time Windows Developers, we are not used to leaving programs running in the background unless we use a minimize button.

    The reason that we want to cleanly exit the program is to allow someone to use the resources used by the program such as a camera when they are finished with our custom application.

    :\

  • KimNieblingKimNiebling DKMember ✭✭
    edited June 2015

    Thanks @IvanHarris. Your solution worked just fine - even with OnBackButtonPressed() on a Login page.

    I have posted my solution to SO:

    http://stackoverflow.com/a/30571114

  • Aniket_SonsaleAniket_Sonsale USMember

    How to close IPhone app using Dependency Service ?

  • andreacurtiandreacurti ITUniversity ✭✭

    Hi @KimNiebling i tried your solution but i got this error:

    but before closing the app, i'd like to ask user for confirmation, any help?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    30jun16 - Real life need due to hardware

    On Android if we connect a USB scanner for the first time, the user is prompted with a permissions screen and the option to make our App the default for the device. The app then has to be restarted for the binding lib to connect. No other way to handle that. The permissions can't be set programmatically. The device can't be white-listed unless you can root the Android device. So saving the app state then restarting would be nice. I would then load the state/settings and pick up where it left off.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @ClintStLaurent can you post your code? I have connected a Usb Printer to an Android Device and the Device visualize a popup that "ask permission".. but I have not to restart the app

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    There's a function in side a jar binding lib that fails if the permissions weren't set when the application was launched. Its not my C# failing, its the java jar inside the Xamarin binding lib wrapper. A couple of specific calls related to the document feeder that fail with the exception in the attached screen capture. Scanner is an industrial model by PDI, model PageScanIV with custom firmware for our company's needs.

  • hello

    Process.GetCurrentProcess().CloseMainWindow();
    Process.GetCurrentProcess().Close();

    seem to do it ( at least from ios) in a xamarin forms project

  • PakasaneeKamutatiPakasaneeKamutati USUniversity

    Thank you @IvanHarris

    I am getting the following unhanded exception on running the app,

    "System.NotImplementedException: The method or operation is not implemented."

    any suggestion or help is appreciated.

  • PaulHoetsPaulHoets USMember ✭✭

    @IvanHarris Very nice code - works great.
    I need it for Android to confirm if you really want to exit from the app on backbutton pressed, and I have no need of it for iOS (navigation-wise, I don't think its needed mostly...?)

    Thanks!

  • NeocomerNeocomer GBMember ✭✭
    edited November 2016

    never mind i posted a duplicate code.

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭

    My use case for this scenario is in a private app, we need to shutdown the app upon several failed login attempts.

  • 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

    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>();
    if (closer != null)
        closer.closeApplication();
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    But again... If you do... Expect the app to be rejected by Google and Apple because it violates their expectations of how a mobile app should operate.

  • knottydevknottydev USMember ✭✭

    @ClintStLaurent said:
    But again... If you do... Expect the app to be rejected by Google and Apple because it violates their expectations of how a mobile app should operate.

    Interesting...

    Are you aware if this is documented anywhere by Google/Apple as a restriction? Do you have a link to said documentation? I've not seen this before / can't find anything.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Its common knowledge among mobile developers. When you submit your app the store will reject it if you have a "quit" feature built in.

  • DennyyDennyy Member ✭✭

    Never heard of it either

  • caseyryancaseyryan Member ✭✭
    edited March 2019

    @ClintStLaurent said:
    Its common knowledge among mobile developers. When you submit your app the store will reject it if you have a "quit" feature built in.

    That's completely not true. I have always built this feature in my apps and games and they had always been approved with no problems.

    @AlessandroCaliaro said:
    App Exit
    From anywhere in your application, you can call Application.Current.Quit() to quit.

    XF 2.5

    It doesn't work. Nothing just happens. Only the solution with dependency services works

Sign In or Register to comment.