0

I have the following CountDownTimer:

countdown = new CountDownTimer(10000, 5000) {
        @Override
        public void onTick(long millisUntilFinished) {
            // Stuff here
        }

        @Override
        public void onFinish() {
            // Restart countdown
            countdown.start();
        }
    }.start();

As seen, there's a 5 seconds tick interval. But when this CountDown starts, the first interval occurs at 0 miliseconds.

Why my first tick is being fired at 0 miliseconds time, instead of 5000 miliseconds time?

Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • CountDownTimer sets the tick all 5 seconds, starting with the first tic at the beginning. To avoid this, make a simple if statement, like if(millisUntilFinished>0){//do Your stuff inside if statement}; – Opiatefuchs Mar 03 '15 at 12:53
  • @Opiatefuchs that won't work, millilUntilFinished is _not_ 0 on `start()` it's "somewhere near" the desired countdown time, but not necessarily always equal to it. – ci_ Mar 03 '15 at 13:01
  • Possible duplicate of [Countdown timer starts from a second after it is supposed to](https://stackoverflow.com/questions/35783313/countdown-timer-starts-from-a-second-after-it-is-supposed-to) – deepGrave Feb 24 '19 at 07:27

1 Answers1

3

All the documentation has to say about onTick() is "Callback fired on regular interval."

http://developer.android.com/reference/android/os/CountDownTimer.html#onTick(long)

Why are you assuming it shouldn't fire immediately?

If want to ignore the first callback to onTick() you could add a boolean flag like so:

countdown = new CountDownTimer(10000, 1000) {
        private boolean first = true;
        @Override
        public void onTick(long millisUntilFinished) {
            if (first) {
                first = false;
                return;
            }
            // Stuff here
        }

        @Override
        public void onFinish() {
            // Restart countdown
            first = true;
            countdown.start();
        }
    }.start();

After looking at your usage of CountDownTimer a bit more it seems like you could also use a Handler / Runnable combination.

Here's an example:

In your Activity you do:

private Handler timerHandler;
private Runnable timerRunnable;

// ...

@Override
public void onCreate() {
    super.onCreate();
    timerHandler = new Handler();
    timerRunnable = new Runnable() {
        @Override
        public void run() {
            // Stuff here
            timerHandler.postDelayed(timerRunnable, 5000);
        }
    };
    timerHandler.postDelayed(timerRunnable, 5000);
}
ci_
  • 8,594
  • 10
  • 39
  • 63
  • That's what I ended doing. A bit ugly, but solves my first tick. I assume is not starting immediately because it says nothing about that. – Sonhja Mar 03 '15 at 13:10
  • @Sonhja I've edited my answer again, you could use a `Handler` / `Runnable` too, might be a bit neater if it fits your needs. – ci_ Mar 03 '15 at 14:02
  • yes, I can use a `Handler` / `Runnable`, but I want to repeat my timer, not only once. So I think I will keep for the moment your first solution. – Sonhja Mar 03 '15 at 14:05
  • If you look at the `Runnable` you'll see that it posts itself again at the end, so I think it is what you want, but whatever you think, it's your question. – ci_ Mar 03 '15 at 14:11
  • Oh I see... Thanks for both solutions! :) – Sonhja Mar 03 '15 at 14:12