1

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 ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • Possible duplicate of [How to close android app in Delphi-XE5 Firemonkey application?](http://stackoverflow.com/questions/19234502/how-to-close-android-app-in-delphi-xe5-firemonkey-application) – Jerry Dodge Sep 24 '16 at 21:16
  • i will take a look, especially on the suggestion SharedActivity.finish :) thanks Jerry – zeus Sep 24 '16 at 22:16
  • i don't understand anything, i compile the classes.dex using the Api 23 framework and now the app close (hardly close - don't wait anything) even if their is running background task :( now the good question what settings make the app hardly close ? i just compile the classes.dex using the Api 23 and use @android:style/Theme.Material.Light.NoActionBar as theme – zeus Sep 24 '16 at 22:39
  • "Hardly close" could mean two opposite things. Do you mean that it does a "Hard Close" or do you mean that it "Barely Closes"? Where do you call the code? It's up to you whether you wish to instruct your app to close or not. If you wish for your app to continue something in the background, then don't tell it to close yet, until it's done. – Jerry Dodge Sep 24 '16 at 23:53
  • The app close when you close the main form, but also wait the finalization of the different unit. it's was like this before. now, and i don't know why, the app close when i close the main form but don't wait the finalization of the different unit, the app stop immediately ... i don't know why it's like this now – zeus Sep 25 '16 at 19:36
  • Well I would expect that it all depends on where in your code you tell the Android to terminate your app. That would be the real question. We can't see where exactly you're calling this code. If you have things that are supposed to run in the background, then you can't expect explicitly terminating your app to automagically know that your background tasks are an exception. You have to work that into your own application. – Jerry Dodge Sep 25 '16 at 20:13

1 Answers1

2

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.
blong
  • 2,145
  • 13
  • 23
  • yes sorry my english is sometime very bad :( i mean, as you understand, forced to close. but as i say, right now when i do main form's close, it's exactly like doing "halt" :( i can't understand why ... but anyway as android is buggy, an application must not take more than 1 s to close (else android crash if you start the application again when it's still in the process of closing), and in this way force closed is not a very bad choice (but i would like to know why suddenly now it's made a force closed, i will investigate deeper, i guess it's maybe because i compile classes.dex using api 23) – zeus Sep 26 '16 at 23:13
  • 1
    There are many pitfalls in English for those not intimate with it, and plenty for those who are. This example is a perfectly understandable case thanks to multiple opposing definitions based on context. Anyway, when you close the main form, `Application.Terminate` is called, which terminates the app. Promptly, by the looks of `TPlatformAndroid.Terminate`. If this occurs when someone presses the Back button on the main form, you could catch that back button and set up a 5 second timer, close things dow0 in the main code, and have the timer call `Application.Terminate`. That might give time. – blong Sep 26 '16 at 23:21