0

I have implemented a 10-1 secs countdown timer in my application.I have declared it globally and instantiated in the onCreate function. the code that i have implemented is :

timer = new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
          mcountdownText.setText("Time Remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
               // my code.....
        }

    }.start();

There is one small problem, when i run my application, i don't know why there are some cases where the countdown timer runs like 10,8,7,6,5,4,3,2,1 (here it skips 9)and sometimes it works proper from 9,8,7,6,5,4,3,2,1 secs. If anyone could help me solve this error, it would be great. Thanks.

Harsh
  • 187
  • 2
  • 2
  • 7

3 Answers3

0

There is no direct answer since the timer should work just fine. The reason might be the process and other tasks that are being executed asynchronously and that interrupts the timer's process. But the timer runs on separate process so chances are it's not the case.

Amir Horev
  • 287
  • 2
  • 3
0

It's a bug of this component.

Here is a good solution. However, in order to resolve it I wrote my solution and if you use RXJava, here you can see another solution using RXJava2

Community
  • 1
  • 1
hooloovoo
  • 825
  • 9
  • 12
-1

maybe you should have a delay before starting the CountdownTimer...

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    timer = new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
          mcountdownText.setText("Time Remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
               // my code.....
        }

    }.start();
  }
}, 1000);  // = 1 sec, you can try less if you want to
Nirel
  • 1,855
  • 1
  • 15
  • 26