How can I tell when someone exits the App

I want to find out how long someone has been in the App.  So my plan is to launch in app startup main.lua…

time_1=os.time()

then whenever the person exits the app I want to record time again…

time_2=os.time()

than (time_2-time_1) to find out user app start time,  finish time, and total time.   So please can someone tell me how can I record the second time when a user exits the app.  Thank you!

You can use a listener for system events such as “applicationSuspend” and “applicationResume”. See here:

https://docs.coronalabs.com/api/event/system/type.html

Thank you for reply, 

so something like this…Runtime:addEventListener(  “applicationExit”, onSystemEvent )

Thank you for reply, however this Gotcha makes it so I can’t use:

When a user “force quits” an application, the “applicationExit” event does not fire on any platform.

so I would lose all times for users who force exit app.

so will “applicationSuspend” work instead?

Sorry, what I said was a bit unclear. There’s just the system event to listen for, but you check for the type as follows:

local function onSystemEvent(event) if event.type == "applicationStart" then -- do stuff elseif event.type == "applicationSuspend" then -- do more stuff elseif event.type == "applicationResume" then -- do other stuff end end Runtime:addEventListener("system", onSystemEvent)

I don’t know what you can do in the case of a force quit, though. I imagine force quits don’t happen too often, so maybe it won’t be a big deal if occasionally it happens? That depends on the specifics of what you’re doing though.

Maybe once a second you could write to a file the length of the session. That way it won’t get lost if the app is forced closed. When the app is opened again, you can read from the file and send it to your server or analytics provider.

local timeStart = os.time() timer.performWithDelay(1000, function() writeToFile(os.time() - timeStart) end, -1)

Obviously you’d need to implement writeToFile() – you could use something like GGData to make this easier.

You could use the app start/suspend/resume events to make this logic smarter and only capture time that the user has your app open.

Aaron thanks I will give that a try!

You can use a listener for system events such as “applicationSuspend” and “applicationResume”. See here:

https://docs.coronalabs.com/api/event/system/type.html

Thank you for reply, 

so something like this…Runtime:addEventListener(  “applicationExit”, onSystemEvent )

Thank you for reply, however this Gotcha makes it so I can’t use:

When a user “force quits” an application, the “applicationExit” event does not fire on any platform.

so I would lose all times for users who force exit app.

so will “applicationSuspend” work instead?

Sorry, what I said was a bit unclear. There’s just the system event to listen for, but you check for the type as follows:

local function onSystemEvent(event) if event.type == "applicationStart" then -- do stuff elseif event.type == "applicationSuspend" then -- do more stuff elseif event.type == "applicationResume" then -- do other stuff end end Runtime:addEventListener("system", onSystemEvent)

I don’t know what you can do in the case of a force quit, though. I imagine force quits don’t happen too often, so maybe it won’t be a big deal if occasionally it happens? That depends on the specifics of what you’re doing though.

Maybe once a second you could write to a file the length of the session. That way it won’t get lost if the app is forced closed. When the app is opened again, you can read from the file and send it to your server or analytics provider.

local timeStart = os.time() timer.performWithDelay(1000, function() writeToFile(os.time() - timeStart) end, -1)

Obviously you’d need to implement writeToFile() – you could use something like GGData to make this easier.

You could use the app start/suspend/resume events to make this logic smarter and only capture time that the user has your app open.

Aaron thanks I will give that a try!