Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

CountDownTimer being executed multiple times even after a check

I have a CountDownTimer in a service, that get's started after a check

if (timerToNextDay == null) {
        timeToNextDay(some long here);
}

the problem is, everytime the service is being initialized and this timer is being checked, it always returns null, even though I am not canceling it anywhere. So after a few starts of the service the timer is being initialized 3 times, I can notice that from this log in its onTick method

Log.i("Hours to next day ", (((millisUntilFinished / 1000) / 60) / 60) + "");

So what can I do to check if the timer is running, to not start it multiple times?

Upvotes: 2

Views: 1760

Answers (1)

Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

To prevent from timer being initialized multiple times, It has to be static, so it should be declared like this

private static CountDownTimer timer;

this ensures that there won't be multiple instances of the CountDownTimer

Upvotes: 4

Related Questions