Page 1 of 1

Detecting app termination

Posted: Tue Jul 26, 2016 5:24 am
by KimD
Hi

I'm trying to re-write my app so that it saves it's state on shutdown. Am I correct in thinking that it is NOT currently possible to detect the termination of an Android app? For example:
- the user hits the recent apps button and then selects the option that allows him/her to remove my app from the list; or
- the user powers off the device while my app is running.

I've tried "On Shutdown", but it doesn't detect either of these events.

Thanks in advance

Kim

Re: Detecting app termination

Posted: Tue Jul 26, 2016 5:27 pm
by MaxV
Did you try this?

Code: Select all

on closeStack -- automatically save changes
  save this stack
  pass closeStack
end closeStack
However if a software is killed from outside, it can't notice it.

Re: Detecting app termination

Posted: Tue Jul 26, 2016 9:19 pm
by KimD
Thanks Max

What I'm after is something that will work if the operating system decides to close (I'm not sure what the correct terminology is) my app. My understanding was that "On CloseStack" would not work in this situation, and I've tested "On Shutdown" and proven that it doesn't work in this situation.

I'm currently thinking that what I need to do is CONSTANTLY save the state of myapp, because:
- there is no way to detect myapp being moved into background (the user pressing the Home or Recent Apps button); and
- there is no way to detect myapp being closed by the operating system.

But before I started coding the constantly save solution, I just wanted to check that I had not mis-understood what I've been reading on the forums about this issue.

Regards

Kim

Re: Detecting app termination

Posted: Wed Jul 27, 2016 12:15 pm
by MaxV
Your problem is a false problem. when same date change, save it.
Use specialFlderPath("documents"), data will be there always, and nobody can touch them.

Re: Detecting app termination

Posted: Wed Jul 27, 2016 5:40 pm
by jacque
The app gets a shutdown message most of the time but I don't know if Android notifies it when the app's in the background. It was my understanding that it should, so there may be a bug in LC's message reporting.

I agree the safest plan is to save after every significant change. Depending on the app that may not need to be after every user action, it may be that saving on every closecard or other milestone is enough.

Re: Detecting app termination

Posted: Wed Jul 27, 2016 9:39 pm
by KimD
Thanks Jacque & Max

1) I couldn't get "On Shutdown" to trigger on Android when I either powered off the phone or terminated my app using the recent apps button. I've since found a note in the Livecode dictionary saying that the most reliable way to implement On Shutdown is in a "backscript" (which I haven't done). Note that I did have more success with triggering "On Shutdown" when I tried the same on IOS.

2) My app now saves its key parameters to a file in the documents folder:
- whenever the user taps the major state changing button; and
- every 5 seconds (while the app is running).
Which is probably overkill, but I'm not noticing any performance degradation.

The next app that I write I'll seriously consider storing the key app state parameters in a txt file INSTEAD of in global variables. What I'm doing now does feel like I'm "doubling up" on storing these.

Thanks again

Kim

Re: Detecting app termination

Posted: Wed Jul 27, 2016 9:52 pm
by jacque
I've since found a note in the Livecode dictionary saying that the most reliable way to implement On Shutdown is in a "backscript"
I think that's just to ensure that the message gets caught, since backscripts always catch everything barring interference upstream. If your stack has nothing else that changes the message path then putting the shutdown handler in the main stack script should work just as well. But it does sound like Android isn't notifying LC in those cases where you aren't getting the shutdown message. I think I'd report the problem in the bug database, just to make sure there isn't a problem with LC itself rather than the Android OS.

But saving as you go is probably the most reliable method. Since saving to a text file is very fast, I'm not surprised you don't notice any slowdown.