applicationSuspend - difference between simulator and hardware?

I was hoping someone might enlighten me why suspending an app in the simulator functions differently than if you suspend it in hardware? For example, if I suspend a game in the simulator it pauses the game as if frozen on an individual frame and picks up straight from there. If I suspend the project on my iPhone3GS by “home buttoning” out of it, the game resumes from it’s base state. Am I doing something different than suspending it in that case? I was under the impression that would suspend the game. I am on an old iOS so maybe that has to do with it?

The interesting thing about the suspend on hardware is that although it “restarts” the game from it’s base state rather than freezing it like it does in the simulator, it restarts with the x and y values initially assigned to my testObject rectangles.

So I guess I’m trying to understand how applicationSuspend and Resume really work and why there’s an inconsistency between my app in the simulator and on hardware.

Thanks! :slight_smile:
(I’m working on the following to test my ability to save and load the state of a game. Clearly I’ve not actually fleshed out my game state functions!)
[lua]-----------------------------------------------------------------------------------------

– main.lua


local moveAmount = 5

local dwidth = display.contentWidth

local testObject = {}

local function makeNewObjects( num )
– body
local number
if num ~= nil then
number = num
else
number = 3
end
for i = 1, number do
testObject[i] = display.newRect(math.random(32, 480 - 32), math.random(32, 320 - 32), 32, 32)
end

end

makeNewObjects()

local function moveObject( … )
– body
for i = 1, #testObject do
testObject[i].x = testObject[i].x + moveAmount
if testObject[i].x > dwidth then
testObject[i].x = testObject[i].x - dwidth
end
end
end
Runtime:addEventListener(“enterFrame”, moveObject)

local function save_state( … )
– body
print(“save_state called”)
end
local function load_saved_state( … )
– body
print(“load_saved_state called”)
end
local function pause_game( … )
– body
print(“pause_game called”)
end
local function resume_game( … )
– body
print(“resume_game called”)
end

local function onSystemEvent( event )
if (event.type == “applicationExit”) then

save_state()

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

load_saved_state()

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

pause_game()

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

resume_game()

end
end
Runtime:addEventListener( “system”, onSystemEvent )[/lua] [import]uid: 105707 topic_id: 29389 reply_id: 329389[/import]

For your app does not start from the begin all the time you press the home button or leave the app you have just to include this line into your build.settings file:

[lua]plist =
{
UIApplicationExitsOnSuspend = false
}[/lua]
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 29389 reply_id: 118098[/import]

UIApplicationExitsOnSuspend = false

This is the default state of Corona build 840. If you have this set to “true” in your build.settings file, this could explain why it starts from the beginning. Otherwise, the simulator and hardware should be the same. The simulator doesn’t use this flag and will always resume where it left off when resumed after suspend.

-Tom [import]uid: 7559 topic_id: 29389 reply_id: 118103[/import]

I had checked the build.settings file as suggested.

It’s set as commented out which Tom has confirmed as defaulting to the false state.
[lua]–UIApplicationExitsOnSuspend = true, – uncomment to quit app on suspend[/lua]

The simulator works as expected (pausing on the frame it’s on and then resuming from there). The hardware does not work this way.

I designed my project to spawn my testObjects at random locations and then move them all uniformly to the right with them “wrapping around” to the left when they move off screen on the right. This seemed like a good way to visually verify the results of saving and loading the state of an object.

The weird thing is that the hardware does “keep” the random values of where the testObjects were spawned, it just doesn’t “freeze” them in place on the frame their at like the simulator does. It “resets” them to the same starting location.

I’m fine with however the system should work, I just want to understand how that should be so I can design appropriately!

If the project should “freeze” on the active frame the way the simulator does on suspend, then it seems like I really only need to solve applicationExit and applicationResume so that I can save and load?

Thanks for your input, I welcome any more feedback! :slight_smile:
[import]uid: 105707 topic_id: 29389 reply_id: 118119[/import]

OK… I seem to have misread this blog post:
http://www.coronalabs.com/blog/2012/07/17/using-system-events-to-save-app-state/

“In case you need to disable the default behavior for whatever reason, for iOS apps you can do so by setting UIApplicationExitsOnSuspend to true in build.settings. The default is false (recommended), which means your apps will resume where they left off automatically (unless the OS quits the app in the background).”

I took the above to mean that I only needed to comment out the following in my build.settings in the case that I wanted to exit on suspend.
[lua]–UIApplicationExitsOnSuspend = true, – uncomment to quit app on suspend[/lua]

In fact I’ve commented this out and set it to false and now my hardware seems to mirror the simulator.
[lua]UIApplicationExitsOnSuspend = false,[/lua]

I guess this seems obvious but it just didn’t occur to me the way I read the post!

Thanks for your input, I wouldn’t have been able to work through this as well as I did otherwise :slight_smile: [import]uid: 105707 topic_id: 29389 reply_id: 118123[/import]

Suspend and resume are easy to test in the simulator, just select the option under the hardware tab. My test functions print their print statements as expected so I can tell that is working correctly.

If I choose to close the project my save_state() function is called and prints it’s print statement to the output window. When I open the project, I do not get the print statement in my load_saved_state() function. Any ideas what I have off? “applicationOpen” is the correct event to trigger that, isn’t it?

[import]uid: 105707 topic_id: 29389 reply_id: 118232[/import]

No, I am amazed, but I have found out that “applicationOpen” is not correct. I looked at the “systemEvents” sampleCode, and then tested out my own, and I found out that instead of “applicationOpen”, it should be “applicationStart”. Try that.

quinc [import]uid: 147322 topic_id: 29389 reply_id: 118234[/import]

@quinc aka binc… wow, nice catch! applicationStart was it. Will have to leave a comment on the Using System Events to Save State post. Thanks :slight_smile: [import]uid: 105707 topic_id: 29389 reply_id: 118236[/import]

Hahaha… I had to change my name, thinking of when I make my first million dollars :slight_smile: and Microsoft Bing sues me. [import]uid: 147322 topic_id: 29389 reply_id: 118237[/import]

Hi. I’ve seen that UIApplicationExitsOnSuspend = false doesn’t work on Android. How can I make it function on Android devices?

Thanks!!

UIApplicationExitsOnSuspend is only an option on iOS.  On Android, I believe the default is that an app suspends when you leave the app by pressing the home button, and an app quits/exits when you leave by pressing the back button.  You may be able to override the former by catching application suspend events and using native.requestExit() to quit the app, though I haven’t tried it myself.  You can override the latter behavior by intercepting back button events.

Hope this helps.

  • Andrew

Didn’t know about the difference between “home” button and “back” button… I’ll also try your suggestion. Thanks a lot Andrew!

Hi. I’ve seen that UIApplicationExitsOnSuspend = false doesn’t work on Android. How can I make it function on Android devices?

Thanks!!

UIApplicationExitsOnSuspend is only an option on iOS.  On Android, I believe the default is that an app suspends when you leave the app by pressing the home button, and an app quits/exits when you leave by pressing the back button.  You may be able to override the former by catching application suspend events and using native.requestExit() to quit the app, though I haven’t tried it myself.  You can override the latter behavior by intercepting back button events.

Hope this helps.

  • Andrew

Didn’t know about the difference between “home” button and “back” button… I’ll also try your suggestion. Thanks a lot Andrew!