Has anyone found out how to close an #XE5 app on #Android? It doesn’t seem to respond to the back button, Self.Close (I only have a single form), or even Application.Terminate: the app stubbornly remains in the air. The CloseQuery event does fire if I call Self.Close, and CanClose is set to True throughout.

Has anyone found out how to close an #XE5   app on #Android?  It doesn’t seem to respond to the back button, Self.Close (I only have a single form), or even Application.Terminate: the app stubbornly remains in the air.  The CloseQuery event does fire if I call Self.Close, and CanClose is set to True throughout.
Am I missing something here?

Comments

  1. The application stays in memory until killed by the os - I'm sure you can exit yourself - have you checked the shared activity object in fmx.helpers.android?

    ReplyDelete
  2. David Kurz Thanks, I knew about halt, but since that doesn't trigger any events (like OnClose), allowing me to save whatever needs saving, or freeing whatever still needs freeing, I’d rather not use that.

    Paul Foster I hadn’t looked at the SharedActivity, perhaps there’s a solution in there. Thanks for the tip!

    ReplyDelete
  3. Its a good starting point, I haven't checked myself but its where I would start looking now :-)

    ReplyDelete
  4. Hm... SharedActivity does provide a finish() method, but that appears to have a similar effect to Halt; the app terminates, but no OnClose event is triggered.

    One difference is that when using Halt, the code in the finally section of my try statement is not executed; whereas that, at least, works normally when using SharedActivity.finish;.

    I’ll see if I can figure out what the usual Android way is of closing the app, and how to react to the app closing (or going into the background).

    It’s a shame, I would have expected that to be handled by the FMX framework. Anyway, I now know how to shutdown my own app, at least. :-)

    ReplyDelete
  5. I suspect that there will be much of this which gets rapidly patched, like the inability to deal with broadcast receivers

    ReplyDelete
  6. Martijn Coppoolse "I’ll see if I can figure out what the usual Android way is of closing the app". The usual Android way is that you don't close the app. If the user presses Home, you are suspended in the background. If they press Back the activity exits. If there was another activity before this one in the app, then that's what you return to. If you were the main activity then that exits and to the user the app has gone. I am not sure that the actual OS process dies, though.
    However, if an Android dev wants to exit an activity, they typically call finish()

    ReplyDelete
  7. SharedActivity.finish look to have the same behaivour of Halt, for me not because onClose not been triggered but Delphi still waits for a correct exit from the app. If I click on Home button Delphi terminates nicely...

    ReplyDelete
  8. If you click on Home the app is supposed to not terminate at all. It is supposed to just go into the background.
    If you find that pressing Home is really terminating your app, then this is errant Android app behaviour.

    ReplyDelete
  9. Brian Long There are quite a few apps out there that do offer an option to terminate them ('press back again to exit', or an explicit 'Exit' option in the menu).
    However that may be, it's still a fact that pressing the Back button often enough will cause me to leave the app I'm in, whatever the app. Often this is simply to go back to the app that opened this one (which will usually be the launcher). That's something an XE5 app doesn't do automatically -- in fact, I couldn't get it to exit at all, until Paul Foster pointed me to SharedActivity.
    I don't often use Home, and it would feel very weird to me if my own app were the only one that I could ONLY quit using the Home button.

    As an aside: if pressing Home really terminates my app, that's probably because my phone is a tad low on memory. When an app goes into the background, there's always a chance that the process will be terminated to make room for the currently active app. This happens quite often on my phone.
    Most Android apps handle this quite gracefully; on restart, they just resume where they left off. In fact, it's only noticeable because normally, some app like a browser or reader keeps their place in a page that's multiple screens high; but when the app has to be reloaded, OTOH, the page is reloaded all right, but I need to scroll down to where I was before.

    Perhaps what Magno Lima refers to, is that the debugger disconnects when the app disappears into the the background. I've noticed that too, even on my tablet which has loads of memory to spare. But the debugger disconnecting doesn't mean that the app has been terminated; in fact, after pressing Home in my app on my tablet, Delphi stops debugging, but  I can just go back to the app, and I can clearly see that it hasn't been restarted.

    ReplyDelete
  10. Martijn Coppoolse "That's something an XE5 app doesn't do automatically" -> What?!
    So you're saying if you make a new Android app, and throw a button on it, launch it on the device and then press Back...., er you don't exit the app and go back to whatever was there before?
    So what does happen then?
    I'm confused. I thought the remaining issues with Back had been resolved before it shipped.
    Any clues as to why it misbehaves from logcat?

    ReplyDelete
  11. Some android apps (dolphin browser mini, for example) support an explicit exit on the app somewhere - I use it often. No, its not the normal android way, but some apps need it - depending on the app it is possible the user doesn't want it kept open. Its besides the point really, the point is you do the activity.finish and the expected behaviour is that form.on close gets fired as happens when a app closes on windoze.

    Incidentally you should be getting the interface for FM android application events and using that - the event that's fired tells you if you going to foreground or background or being terminated.

    ReplyDelete
  12. Brian Long "So you're saying if you make a new Android app, and throw a button on it, launch it on the device and then press Back...., er you don't exit the app and go back to whatever was there before?
    So what does happen then?"

    In the case of my app: nothing happens. That's why I was looking for a way to explicitly close it (or push it to the background, or whatever).

    But that's a fair point: I haven't tested it with an (almost) empty form. I'll try that tomorrow, and will check if there is perhaps a component on there that's cancelling the close, or intercepting the Back button; I've been experimenting with Livebindings and FireDAC, so I have quite a few components on the form (visual as well as non-visual). There's almost no code behind the form, that's why  I assumed it just wasn't supported by XE5's FMX framework.

    ReplyDelete

Post a Comment