"applicationExit" event called on application startup?

I am having a problem with handling system events. It appears that the “applicationExit” event is being called on startup when I run my application in the corona simulator, when it should only be called when the application exits (i.e. I close the corona simulator). I am trying to use the “applicationExit” event to save user data when the application exits, so it is critical that the “applicationExit” event only be called when the application exits. Below is a sample of my code:

--main.lua local function onSystemEvent( event ) if ( event.type == "applicationOpen" ) then if box.exitCount == nil then defaultValues() else setValues() end elseif ( event.type == "applicationExit" ) then --problem: seems to run "applicationExit"" on startup print("application is exiting!!!!!") if box.exitCount == nil then print("first time opened") else box.exitCount = box.exitCount + 1 print( "exitCount = "..box.exitCount) end saveBox() --printBoxValues() elseif (event.type == "applicationSuspend") then --do nothing end end Runtime:addEventListener( "system", onSystemEvent )

When I run my code on the corona simulator, the “application is exiting!!!” text appears in the output for about a second or two and then disappears, indicating that the “applicationExit” event was called on startup when it should only be called when the application exits. Is there a way to prevent the “applicationExit” from running on startup? Any help in addressing this problem would be much appreciated!

Have you confirmed that this happens on the device as well?  I wonder if it is a simulator issue.

Also, just curious as to why you are using applicationOpen?  I myself would prefer to use both applicationStart and applicationResume to test if the app is opened. 

I just tried running your code (removing the parts about saving boxes), and I’m not seeing the issue you’re describing.  I’m using build 1206 on Windows.

[lua]

–main.lua

local function onSystemEvent( event )

   if ( event.type == “applicationOpen” ) then

      print(“application is open!!!”)

   elseif ( event.type == “applicationExit” ) then

      print(“application is exiting!!!”)

        

   elseif (event.type == “applicationSuspend”) then

      print(“application is suspending!!!”)

   end

end

Runtime:addEventListener( “system”, onSystemEvent )

[/lua]

Is the “application is exiting!!!” text printing even when you launch the Simulator from a fresh start?  The reason I ask is that if you’re relaunching the project once the Simulator is already open and running, then naturally you’ll see the exit text fire for when the currently running process exits.

  • Andrew

Thanks for the feedback! I think the problem was with my version of the corona simulator, because I tried uninstalling and reinstalling the latest version of the corona simulator (build 2013.1202) and did not have the same problem after that. Before doing this though, I did try completly exiting and restarting the corona simulator and my IDE, before running the app to see if that would solve my problem, but the problem still persisted.

Also, thanks for suggesting using “applicationStart” and “applicationResume” system events. I looked up “applicationOpen”, and that seems to be only used for checking when URL’s are opened rather than when the application itself is opened, which is not what I needed. This could also help explain my original problem which was that data was not being saved properly because it was not retrieved when the application was opened (when the “applicationStart” event occurs).

Have you confirmed that this happens on the device as well?  I wonder if it is a simulator issue.

Also, just curious as to why you are using applicationOpen?  I myself would prefer to use both applicationStart and applicationResume to test if the app is opened. 

I just tried running your code (removing the parts about saving boxes), and I’m not seeing the issue you’re describing.  I’m using build 1206 on Windows.

[lua]

–main.lua

local function onSystemEvent( event )

   if ( event.type == “applicationOpen” ) then

      print(“application is open!!!”)

   elseif ( event.type == “applicationExit” ) then

      print(“application is exiting!!!”)

        

   elseif (event.type == “applicationSuspend”) then

      print(“application is suspending!!!”)

   end

end

Runtime:addEventListener( “system”, onSystemEvent )

[/lua]

Is the “application is exiting!!!” text printing even when you launch the Simulator from a fresh start?  The reason I ask is that if you’re relaunching the project once the Simulator is already open and running, then naturally you’ll see the exit text fire for when the currently running process exits.

  • Andrew

Thanks for the feedback! I think the problem was with my version of the corona simulator, because I tried uninstalling and reinstalling the latest version of the corona simulator (build 2013.1202) and did not have the same problem after that. Before doing this though, I did try completly exiting and restarting the corona simulator and my IDE, before running the app to see if that would solve my problem, but the problem still persisted.

Also, thanks for suggesting using “applicationStart” and “applicationResume” system events. I looked up “applicationOpen”, and that seems to be only used for checking when URL’s are opened rather than when the application itself is opened, which is not what I needed. This could also help explain my original problem which was that data was not being saved properly because it was not retrieved when the application was opened (when the “applicationStart” event occurs).