Answer the question
In order to leave comments, you need to log in
How to create a timer at the end of which lives are restored?
Hello, I have an application, like a quiz, I need to make a system of lives, or rather, their restoration. When there are less than three lives, a timer should start for an hour and count down by a second, this should be displayed in the window with lives.
After I exit the application, I need the timer to continue running.
I've been googling for the second day, I found only timertask, but I don't quite understand how to work with it. I will be grateful for your help! Perhaps the solution is quite simple, but I do not understand, just started reading about android. (1.5 months)
Answer the question
In order to leave comments, you need to log in
At the moment when the countdown should start, store the current time in persistent storage, and also start the countdown in an arbitrary way (for example, postDelayed, or Observable::interval). At the next countdown step, count how much time has passed between the moment you saved and the current moment. Stop the countdown when the application stops (for example in Activity::onStop, Fragment::onDestroyView, etc).
When restoring the state, simply count how much time has passed, and either start the countdown again, or do what should be after the countdown.
Something like this. Of course, all this should be beautifully encapsulated, it's just the main idea.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question