Does event.type "applicationExit" actually work?

How does one detect when an app has been exited by a user touching the home button on the iPhone?

I have these lines in an app to test the applicationExit condition but nothing happens if I quit out of the app using the home button.

[lua]local function onSystemEvent (event)
if event.type == “applicationExit” then
system.vibrate()

Runtime:addEventListener(“enterFrame”, onSystemEvent)[/lua]

[import]uid: 1560 topic_id: 2604 reply_id: 302604[/import]

You can’t do much when you get an applicationExit event. You can save data to a local file but that’s about it.

Check out the SystemEvents sample code in the Corona SDK for an example that shows how to detect if the applicationExit event was received. It writes a file on exit and detects the file at startup.

-Tom [import]uid: 7559 topic_id: 2604 reply_id: 7396[/import]

I started out using the System Events code and I have the saving and loading working when triggered manually in the app. But I can’t get the applicationExit event to do anything…print or vibrate the phone or save the data. Likewise the applicationStart event isn’t doing anything.

Might be it related to the placement of the listener in the code? IE: if the listener doesn’t fire early enough the applicationStart event gets missed?

[lua]local function onSystemEvent (event)
–print (“onSystemEvent”)
if event.type == “applicationExit” then
–system.vibrate()
–print (“Exiting app, saving data”)
–flashText (“Exiting”)
– show initial data
packData()
print(“Data before saving:”)
for k,v in pairs( dataTable ) do
print( k … “=” … v )
end
saveData()

elseif event.type == “applicationStart” then
print (“applicationStart”)
system.vibrate()
if shouldResume () then
print (“Load data”)
flashText(“Load data”)
loadData()
– do something with loaded data
print(“Data after reloading:”)

for k,v in pairs( dataTableNew ) do
print( k … “=” … v )
end
else
print (“Clean start”)
flashText(“Clean start”)
end
end
end[/lua] [import]uid: 1560 topic_id: 2604 reply_id: 7408[/import]

As an alternative, is there a way to intercept a touch that lands on a Web popup to do a save before Safari opens and kills the app? [import]uid: 1560 topic_id: 2604 reply_id: 7409[/import]

Yes, check out the Admob sample, I do exactly this to store if the user clicked on the ad. [import]uid: 5712 topic_id: 2604 reply_id: 7418[/import]

The SystemEvents code works fine with all the Application events and is able to create a file upon exit. You might want to go back to that code and make changes one by one and see where it stops working.

-Tom [import]uid: 7559 topic_id: 2604 reply_id: 7442[/import]

Update: Problem resolved.

I’m not sure exactly which change fixed the issue but combining two functions (packData() and saveData()) into a single function and dropping the local declaration for both function saveData() and function onSystemEvent (event), the applicationExit routine started saving the game file.

Thanks for the help!

#

Sadly, the Admob code doesn’t seem to like a call to save data near a URL request:

I tried this:

[lua]local function showAd(event)
– Is the url a remote call?
if string.find(event.url, “http://”, 1, false) == 1 then
– Is it a call to the admob server?
if string.find(event.url, “c.admob.com”, 1, false) == nil then
adSpace.url = event.url
else
– an actual click on an ad, so open in Safari
saveData() – this is the only altered line from the standard Admob code
system.openURL(event.url)

end
else
adSpace.url = event.url
end
end[/lua]

and the Safari no longer launched. Instead, the window opened as a banner on the bottom of the screen. No data was saved.

Moving the saveData() function around didn’t help either. Putting it at the bottom restored the Safari launch but still no data was saved.

saveData() works fine for me when triggered by a button. [import]uid: 1560 topic_id: 2604 reply_id: 7491[/import]

Which OS and device are you testing it? [import]uid: 5712 topic_id: 2604 reply_id: 7559[/import]

iPhone 4, iOS 4.1

If I had to guess, the issue may have been having more than one function call in applicationExit. When I’m doing more builds I may try making the onSystemEvent function local again to see if I can eliminate that as a possibility. [import]uid: 1560 topic_id: 2604 reply_id: 7586[/import]