Is the app in the background?

Is there a way to tell if the game is in the background, specifically in Android?

Mmkay so since I’m not sure how to tell if the app was sent to the background through being a programmer, I came up with a simple solution in the event sheet that works retrospectively to allow my project to deduce that it had been paused, once it is running again. This way I am able to correctly compensate the player that had sent the app to the background or had locked the screen with the app running, with the appropriate “offline earnings” that would have been afforded to them had the app just been killed for that same amount of time.

My initial thought was to use this event to trigger a “change to scene” and restart the scene that way, where my beginning of scene events would take care of the “offline” calculations and reward the player appropriately. And this did work great if the app was sent to the background and resumed later.

However, the lock screen was a different story entirely. From the “offline” calculations that were being done with the app running - then screen locked - then resumed, I could only come to the conclusion that the way Android handles lock screen events is different and was conflicting with my auto save on certain events, or conflicting with something else, I never pinpointed what. But the result was when I finally unlocked the screen (anything over a minute of locking it) to return to the game, (most of the time) the timestamp was now the time I unlocked the screen so I earned nothing the entire time I was gone.

So instead I used this event to simply run a copy of the begin of scene events that deal with offline earnings. This has been working great for both lock screen and background resumes. And if someone does something similar, they can try just restarting the scene first, as that would probably work well in most situations that it is not conflicting with something else you’re doing.

In the future, being aware that Android might not kill the app but pause it instead, it should be a minor thing to write “game running” logic to be more like “beginning of scene” logic where needed, such as more loopy and less linear when comparing time now to number of products that should have been produced. However, though I nearly did that in this project since I didn’t know how to tell when it was pausing, I decided I would really like a message saying “While you were away you earned $$” so I just continued trying to find a way to detect a pause.

The message will fix the dreaded “pinch zoom bug”, where the first touch after resuming the app counts as the second touch to a “frozen ghost touch” and zooms out uncontrollably. The message in a GameState that does not allow zooming will fix that by having the user press Ok and normalizing the touches before the GameState changes to allow zooming. I hope.

Oh also I’m leaving this open in case some day someone does know how to do this “really” and not “creatively” and can answer it.