1

I am writing an HIIT (High Intensity Interval Training) activity for which I am implementing an interval timer. The CountDownTimer is supposed to finish 5 minutes of Warm-Up, then proceed to time the HIIT workout.

 public class WarmUpActivity extends ActionBarActivity{

    TextView Mode;
    TextView Time;
    int minutes;
    long time_remaining;
    boolean warmup_finished;
    private CountDownTimer HIIT_Timer;


    private void StartTimer() {
         HIIT_Timer = new CountDownTimer(time_remaining, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time_remaining = millisUntilFinished; //in case activity is paused or stopped
                Time.setText(" " + (int)floor(millisUntilFinished / 60000) + ":" + ((millisUntilFinished / 1000) % 60));

                if (warmup_finished == true) { //if we are in HIIT mode
                    if ((int)millisUntilFinished % 60000 == 0) { //every minute
                        if (Mode.getText() == "Low Intensity")
                            Mode.setText("High Intensity");
                        else
                            Mode.setText("Low Intensity");
                    }
                }
            }

            @Override
            public void onFinish() {
                if (warmup_finished==false){
                    Mode.setText("Low Intensity");
                    warmup_finished = true;
                    HIIT_Method();
                    return;
                }
                else  {
                    Completed_Method();
                    return;
                }

            }
        }.start();
    }


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hiit_layout);

        Mode=(TextView) findViewById(R.id.mode);
        Time=(TextView) findViewById(R.id.time);

        warmup_finished=false;
        Mode.setText("Warm-Up");
        time_remaining=5*60000; //5 minutes when created

        }

    @Override
    public void onStart(){

        super.onStart();
        StartTimer();
        return;
    }


    private void HIIT_Method(){
        minutes=getIntent().getIntExtra(SelectHIITDuration.MINUTES, 0);
        time_remaining=minutes*60000;
        StartTimer();
        return;
    }

    private void Completed_Method(){
        Mode.setText("Workout Completed");

    }
}

When warmup is finished and onFinish() is called for the first time, HIIT_Method is called, in which the HIIT timer is supposed to start with user specified duration. The problem is, after the new timer is declared using Start_Timer(), somehow Completed_Method is called. It can only be called from onFinish(). Why is onFinish() being called after I declare a new timer?

Forketyfork
  • 7,416
  • 1
  • 26
  • 33
  • Have you tried calling your initial start_timer in oncreate instead of onstart? Not sure if this will solve anything. – vkuo May 28 '15 at 16:53
  • Thank you. This worked. My new timer starts without going to Completed_Method() . I am not sure why this worked. An explanation would be much appreciated as I am new to android development. – JackOfAllTrades May 28 '15 at 18:04
  • sent in answers, let me know if this is satisfactory. – vkuo May 28 '15 at 18:18

1 Answers1

0

We need to move your call to startTimer from onStart to onCreate.

The issue here is understanding the Android lifecycle for an activity. A very good explanation by someone with better understanding than I is explained here.

Now from what I understand, we typically don't touch onStart until we start worrying about service binding and database queries, but other developers may think differently.

The official android documentation on an activity's lifecycle can be found here

Community
  • 1
  • 1
vkuo
  • 350
  • 1
  • 4
  • 21