Please see my original question here:
Using System Time to compute delay times that correctly deal with app pauses?
In summary, I am attempting to to introduce a delay (say of 5 seconds or so) before a sprite takes a particular action (say a jump) - but this needs to be preserved when the user pauses the game or the system kills the app and it is re-launched. I tried to carry out the suggestion in the answer to my previous question (to save the time on app pause and then compare it to current time when resumed to see how long the app was inactive for, then to add this to the time that I check against, but this comes with it's own problems and I'm still not sure of the best approach).
When the app is re-started following the system trashing it to reclaim resources, the time (now correctly adjusted to include time the app was sitting in the background). I reset & compare the time when restoring the bundle. However, it then takes 4 or 5 seconds to re-load everything (this is an OpenGL app, so resources are loaded in onSurfaceCreated which comes after the Bundle is restored) so by the time the game starts, vital seconds have already passed (so, lets say when resuming there should be 4 seconds remaining before a sprite appears, it will still appear instantly).
As mentioned, when the app goes into the background, I'm storing the time and then comparing it to the time when the bundle is restored to get the 'app not active' time - however, what happens in cases when the user (or system) sends the app into the background but the app stays active, and is then re-launched? the Bundle is then never restored, so I can't grab the restore time to do the comparisons.
So how best to deal with sprite delays which won't break when the app is in the background and not running? Basically I seem to need a way to have a timer that can be stopped when the app is paused or not active. (The countdown method I described in the original question seems to be the closest thing, ie, just keep a value of say 100 and decrease it each time a game-tic occurs, this would be a lot easier and tidier, however, it would also mean if the game were to ever run slower than 60 ticks per seconds, the delays would be longer), but is there someway I could use 'real' time rather than something anchored to my tics per second that wont' carry on merrily when the game isn't running?
Code for delay based on System time (this is in my game logic update)
//Timer is initially set to 0, so grab time to start timer
if (timer == 0)
timer = System.currentTimeMillis();
//Delay completed? (5 Seconds)
if (System.currentTimeMillis()>Timer+5000){
//Reset for next delay
levelTimer = System.currentTimeMillis();
//Carry out required action here (example, jump or more sprite)
}
Code to reset and re-adjust the timer (currently in onRestoreInstanceState)
timer+=(System.currentTimeMillis()-pauseDuration);