In a game I am developing I want to create a delay. The delay am I makeing is using a handler with this code:
handler.postDelayed(new Runnable() {
public void run() {
animationDrawable.start();
}
},theDelay);
The problem is that I got a pause button that is going to make everything stop, and not do anything until you resume again. For now it is running even though the gameplay is paused. I got a boolean that is changed when I want to pause the activity.
I have searched for solutions using a Thread, TimerTask and Handler, but for everyone of these I have seen warning about that non of these is safe to use.
How can I pause and resume this “countdown” in a secure way, and without haveing to start it all over again when resuming?
startTime = System.currentTimeMillis();onPausesave that timepauseTime = System.currentTimeMillis();and cancel the hendleronResumerecreate the event with thepauseTime - startTime