Detecting app termination

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Detecting app termination

Post by KimD » Tue Jul 26, 2016 5:24 am

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

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Detecting app termination

Post by MaxV » Tue Jul 26, 2016 5:27 pm

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.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Re: Detecting app termination

Post by KimD » Tue Jul 26, 2016 9:19 pm

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

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Detecting app termination

Post by MaxV » Wed Jul 27, 2016 12:15 pm

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.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Detecting app termination

Post by jacque » Wed Jul 27, 2016 5:40 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Re: Detecting app termination

Post by KimD » Wed Jul 27, 2016 9:39 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Detecting app termination

Post by jacque » Wed Jul 27, 2016 9:52 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply