Search code examples
javaandroidcountdowntimer

CountDownTimer strange Finish behaviour


When Timer Finish it goes back to previous Activity for a Second then Apply onFinish What is the reason and how to prevent that.

mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        mTimeLeftInMillis = millisUntilFinished;
        updateCountDownText();
    }

    @Override
    public void onFinish() {
        playAgain();
    }
}.start();

Solution

  • The Reseaon is at the start of every tick, before onTick() is called, the remaining time until the end of the countdown is calculated and If this time is smaller than the countdown time interval, onTick() would not not called anymore. When onTick() called CountDownTimer consume some time for calculation. the easiest way to handle this is to add some extra timeLeftInMillis to your timer!