Howto Close/Quit a MobileApplication?

Does anybody know how to close a MobileApplication (Android)?

QUIT()
or App.QUIT()

do not exist in MobileApplications!?!
Maybe the solution to this problem is too simple for me to see?

Mobile applications don’t have the paradigm of closing. Users control this with the application switcher.

Why do you need it?
There are only some very special cases where closing a mobile app is needed, such as changing the language for the user on an iOS device.

On Android, I haven’t found a use case for closing an app yet.

Like for example if the user does not agree to the usage terms (this is the case here).

Other cases could be if there are unrecoverable internal errors or
if the user is trying to brute force break a password (and/or is creating a high server load with the Mobile Client)

You don’t exit for this, you show another screen that says “To use this application you must agree to the terms.” A button that allows the user to return to the terms would be highly recommended.

Again, a screen showing details about the error. If the application becomes truly unstable, the system will close it. Paul’s comment about App.UnhandledException substantiates that

This isn’t going to happen in your app in a way that you can’t manage it with the UI. Disable a button and show a message. If someone is really trying to brute force a password they will do so by reverse engineering the API.

You need to update your thinking to design for a mobile app, they behave differently than desktop applications. Mobile apps don’t quit.

2 Likes

All that said, you could probably raise an exception and make it crash. That would effectively exit the app. :stuck_out_tongue:

2 Likes

DIM i AS Integer = 1 / 0

:joy:

2 Likes

:grinning:

Actually, I don’t think dividing by zero will cause an exception, so this may not have the desired effect.
This, however, will:

raise new RuntimeException

1 Like

While this is indeed the expected way, I know a few apps that can close by themselves (on Android), so there must be an API for that.

2 Likes

for example banking & financial application needs to close

2 Likes

To close an app on Android, all you need is to raise an exception.

On iOS, you can either raise an exception and return false from App.UnhandledException or use this code

Declare Sub os_exit Lib "/usr/lib/libSystem.dylib" Alias "exit"(ret As Int32)
  os_exit(0)
1 Like

If you choose to “quit” by crashing the app know 2 things -

  1. If Apple triggers the crash during review your app may be rejected
  2. Crash reports you get back from users through Apple will include instances you intentionally crashed, which could confound debugging of real problems in your app

As Tim has suggested, consider changing the user workflow so that intentionally crashing the app is not required

2 Likes

Why? I’ve never heard anything about that.
Since it wouldn’t make it safer, it can’t be related to security.

Exactly: Technical Q&A QA1561: How do I programmatically quit my iOS application?

Android has APIs to make it gracefully end and release its memory without firing a crash that may call attention for the quality of your app. Quit(code) could exist in Xojo if Xojo wanted to. And you could raise a Xojo compiler warning like “Quitting mobile apps is unusual” just to teach people.

Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.

I’ve never met one that does. They logout but they never quit. I’ve about 5 bank / credit card apps and none of them ever quit.

1 Like

To answer the original question: For Android you can use Declares:

Public Sub Quit()
  #If TargetAndroid
    
    Var myScreen As MobileScreen = App.CurrentScreen
    
    Declare Sub finishAndRemoveTask Lib "Object:myScreen:MobileScreen"
    Declare Sub quitApp Lib "Kotlin" Alias "System.exit(0)"
    
    finishAndRemoveTask
    quitApp

  #EndIf
End Sub
2 Likes

My banking app does logout and quit. Both Android & Iphone apps.

I think that an app may have multiple tasks and you may need to send a finish…() to each one of those existing tasks in a proper order (in case of some dependency) before the quit()