Search code examples
androidcountdown

Countdown timer not looping


Basically I am doing a cardio feature and have three countdown timers in a row nested within each other, so when one timer finishes, the next one starts. One for preparation time, one for workout time and one for rest time, the user chooses the times of these.

I need it to loop however many times the user selects from a numberpicker, but no matter what I do it only goes through it once and doesn't loop so I know it all works it's just the looping part that doesn't work.

Am I missing something here? Is there a better way to do this?

    //Main countdown timers loop
    for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
    {
         prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {

             public void onTick(long millisUntilFinished) {

                 tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                 if(millisUntilFinished <= (6 * 1000))
                 {
                     tvCountDown.setTextColor(Color.RED);
                 }
             }

             public void onFinish() {
                 workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {

                     public void onTick(long millisUntilFinished) {
                         tvCountDown.setTextColor(Color.GREEN);
                         tvCountDown.setText((millisUntilFinished / 1000) + "s");
                         if(millisUntilFinished <= 6 * 1000)
                         {
                             tvCountDown.setTextColor(Color.RED);
                         }
                     }

                     public void onFinish() {
                         restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {

                             public void onTick(long millisUntilFinished) {
                                 tvCountDown.setTextColor(Color.GREEN);
                                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                                 if(millisUntilFinished <= 6 * 1000)
                                 {
                                     tvCountDown.setTextColor(Color.RED);
                                 }
                             }

                             public void onFinish() {
                                 roundCount = roundCount + 1;
                             }
                          }.start();
                     }
                  }.start();
             }
          }.start();

    }

Solution

  • the issue here is that you create prepCountTimer and assign on finish ect, then start it. then it reaches the end of for each and loops again making another preopCountTimer and starting it. you need to make your restCountTimer start the next preopCountTimer once it's done. unless I'm understanding something wrong here.

    public void callingMethod() {
        timerMethod(times.getValue());
        // execution continues as your timer will run in a different thread
    }
    
    public void timerMethod(final int count) {
        if (count == 0) {
            // we have done the number of timers we want we can
            // call whatever we wanted to once our timers were done
        }
        //you could use count to get the times for each timer here
        startTimer(_finalPrep, new timerListner() {
            @Override
            public void timerFinish() {
                //when timer 1 finishes we will start timer 2
                startTimer(_finalWorkout, new timerListner() {
                    @Override
                    public void timerFinish() {
                        //when timer 2 finishes we will start timer 3
                        startTimer(_finalRest, new timerListner() {
                            @Override
                            public void timerFinish() {
                                //when timer 3 finishes we want to call the next timer in the list.
                                timerMethod(count - 1);
                            }
                        });
                    }
                });
            }
        });
    }
    
    private interface timerListner {
        void timerFinish();
    }
    
    public void startTimer(int timerTime, final timerListner onFinish) {
        // you can pass in other parameters unqiue to each timer to this method aswell
        CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
            public void onTick(long millisUntilFinished) {
                tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
                tvCountDown.setText((millisUntilFinished / 1000) + "s");
                if (millisUntilFinished <= (6 * 1000)) {
                    tvCountDown.setTextColor(Color.RED);
                }
            }
    
            @Override
            public void onFinish() {
                onFinish.timerFinish();
            }
        };
        timer.start();
    
    }