Clarification on System Events

I am trying to troubleshoot a problem on the Android version of an app.  I think it has to do with system events.  As a test I created the following bit of code:

[lua]

local txt = display.newText(“App Running”, display.contentCenterX,

         display.contentCenterY, native.systemFont, 16)

local function onSystemEvent(event)

    print ("Event: "…event.type)

end

    

Runtime:addEventListener(“system”, onSystemEvent)

[/lua]

When running this in the simulator, and simulating a suspend, I get the following output, which I expected:

[lua]

Windows simulator build date: Feb 13 2014 @ 02:24:13

Event: applicationStart

Event: applicationSuspend

Event: applicationResume

[/lua]

I compiled the code and placed in on my Android device (Galaxy S3), then fired up logcat. I did 3 tests – all with the same results:

  1. Let the screen time-out to black, then pressed the side button.
  2. Let the screen time-out to black, then pressed the home button.
  3. Press the side button to time-out screen, then pressed the home button.

For each test, I got the following output (disregarding the line numbers, of course):

[lua]

    Line 3591: I/Corona  ( 8861): Event: applicationStart

    Line 5901: I/Corona  ( 8861): Event: applicationSuspend

    Line 6461: I/Corona  ( 8861): Event: applicationResume

    Line 6785: I/Corona  ( 8861): Event: applicationSuspend

    Line 6893: I/Corona  ( 8861): Event: applicationExit

    Line 12443: I/Corona  ( 9423): Event: applicationStart

    Line 12713: I/Corona  ( 9423): Event: applicationSuspend

    Line 14327: I/Corona  ( 9423): Event: applicationResume

    Line 14555: I/Corona  ( 9423): Event: applicationSuspend

    Line 14705: I/Corona  ( 9423): Event: applicationExit

    Line 18827: I/Corona  ( 9615): Event: applicationStart

    Line 19005: I/Corona  ( 9615): Event: applicationSuspend

    Line 20587: I/Corona  ( 9615): Event: applicationResume

    Line 20805: I/Corona  ( 9615): Event: applicationSuspend

    Line 20945: I/Corona  ( 9615): Event: applicationExit

[/lua]

Why is this happening?  I didn’t expect 3 cycles for each test… that’s not normal, is it?  Why is suspend being executed right after a resume?  

Am I not understanding how to use this?

Any help appreciated. Thanks!

–John

Do you have a screen lock?

Actually, yes I do! Interesting. Are you saying that this is the cause? If screen lock causes all of these events, how do I code around this? I am sure others would have that as well.

I have seen that sometimes when you unlock the phone it will show the last screen first and only after show the screen lock. That might be what’s happening.

I’m not sure how to fix it though except maybe resuming your app only after a small delay and it has not been suspended again.

Thanks for the info… now that i know why this is happening i can figure out a way to code around this.

I’m not saying 100% that that is it. It might be.

Understood. I’ll do a bit more testing but it does gove me an area to look into. Thanks for your help.

Do you have a screen lock?

Actually, yes I do! Interesting. Are you saying that this is the cause? If screen lock causes all of these events, how do I code around this? I am sure others would have that as well.

I have seen that sometimes when you unlock the phone it will show the last screen first and only after show the screen lock. That might be what’s happening.

I’m not sure how to fix it though except maybe resuming your app only after a small delay and it has not been suspended again.

Thanks for the info… now that i know why this is happening i can figure out a way to code around this.

I’m not saying 100% that that is it. It might be.

Understood. I’ll do a bit more testing but it does gove me an area to look into. Thanks for your help.