loki loki - 1 year ago 535
Android Question

delphi how to force app close under android

their is a problem under android, if we close the app and try immediately to reopen it, then if it's still in the process of closing we get a "white screen of death". It's quite normal i think, android try to show the running app because he maybe don't know that the app is in the process of finishing.

Normally the app must close immediately but sometime their is some background tasks running that don't close immediately (like http connection, file in process of writing to the drive, etc.). Is their a way to don't care of anything and hardly close the app ?

Answer Source

Just a point on the ridiculousness of English words that can mean either what they ought to or the polar opposite based on context: the word 'hardly' in this context doesn't mean 'aggressively', it means 'barely' or 'scarcely', but I assume from hereon in that you mean 'hardly close' to mean 'forced to close'.

It seems that Application.Terminate is the "official way", at least in more recent versions of Delphi. Looking at the implementation in TPlatformAndroid.Terminate it does what ought to be done on closing.

Note that in XE5 (including the Update Pack) the TPlatformAndroid.Terminate method was empty. Delphi XE6 to XE8 has the basics of setting IFMXApplicationService.Terminating to True, terminating timers and instructing the underlying native activity to finish with a call to ANativeActivity_finish. Delphi 10 Seattle added to this by triggering TForm.OnSaveState and Delphi 10.1 Berlin takes steps to ensure all this code is run safely in the FMX thread.

There are other options, several of which have various drawbacks. The drawback of some of the seemingly obvious candidates is the production of runtime errors (・_・、)

Anyway, you could try these options if you wanted:

  • call the main form's Close method -> seems to work in recent versions
  • call the main form's Release method -> produces an EListError with message Unbalanced stack or queue operation if called from a main form method thanks to a marked difference between the behaviour and implementation of Release on Windows and on Android
  • call DisposeOf against the main form -> produces an Access Violation if called from an event handler thanks to its immediate efect - typically not a good result
  • call the finish method of the underlying Android activity by calling TAndroidHelper.Activity.finish (which relies on the Androidapi.Helpers unit) or MainActivity.finish (which relies on the FMX.Platform.Android unit) -> seems to work
  • call Halt -> this is a most abrasive manner in which to terminate your application, which is not recommended: the app will abruptly end bypassing any important cleanup code, etc.
  • call kill(getpid, SIGKILL) after using the Posix.Pthread, Posix.Unistd and Posix.Signal methods -> another extremely abrasive method (possibly the most abrasive), which is not recommended: the app will abruptly end bypassing any important cleanup code, etc.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download