1
\$\begingroup\$

I want my android game using LibGDX to continue keeping track of events in the game, even while it's not the active window.

For example, I have a stat that reduces over time even, if the game is not the active activity. When the stat gets to a certain percentage, I want to push a notification to the android platform.

I found some information to push android notifications. However, I could not find one on how to make a stat persist in the game when a different app or activity is on the main screen.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to GameDev! Please note that we consider tutorial requests off topic. I have removed your tutorial request, and left your question as "How do I do X". We can still try and answer this, for you, and with luck a good answer may still provide a good tutorial for extended reading. \$\endgroup\$
    – Gnemlock
    Jan 31 '17 at 0:01
2
\$\begingroup\$

The way that other games do it is by frequently saving the current time to disk then loading it on startup and subtracting it from the saved time. That way you know the duration that the app was off.

When the app is closed or minimised android will call OnPause, OnStop or/and OnDestroy. At which point you quickly grab the system time and save it. You can read more about Android application lifecycles Here. Then when OnStart or OnResume is called you simply read that value from file and work out what timers need to be changed or if they are done.

Hope this helps :)

\$\endgroup\$
2
  • \$\begingroup\$ Ah, very clever. I didn't think about that. I'm sure this would work in what I'm trying to do with a few work arounds. Thanks, User3797758 \$\endgroup\$
    – crazydaze
    Jan 31 '17 at 0:31
  • \$\begingroup\$ Awesome glad I could help and welcome to the gamedev stack overflow. Since you're new here i thought i might tell you that if you have an answer to a question that you think answers it you should press the little tick under the answer so the question is marked as answered (it gives you a few points on here which helps you get more privileges). \$\endgroup\$ Jan 31 '17 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.