1

in my app I am using a CountDownTimer and would like users to be able to reset the data in the app and start fresh. The problem I am having is that if you start a timer on say, 3 minutes and then a short time later you call my reset function to set all scores back to their initial values, and then start a new timer at say, 10 minutes.... the timer immediately picks up where it left off saying 2:24, 2:23, 2:22.....

Everything I have read everywhere says that you call countdowntimername.cancel(); and then set countdowntimername = null;

This is how I am doing it, and this is not working at all. There must be a more effective way to kill these timers? Any ideas?

Sauce:

private void toggleTimer() {
    if (timerStarted == false) {
        final CountDownTimer countDownTimer = new CountDownTimer(startTime, interval) {

            public void onTick(long millisUntilFinished) {
                    if (timerPaused == true) {
                        cancel();
                    } else {
                        timeRemaining = millisUntilFinished;
                        long remainder = millisUntilFinished;
                        minuteTens = df.format(millisUntilFinished / 600000);
                        remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
                        minuteOnes = df.format(remainder / 60000);
                        remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
                        secondTens = df.format(remainder / 10000);
                        remainder = remainder - (Integer.valueOf(secondTens) * 10000);
                        secondOnes = df.format(remainder / 1000);
                        setCountdownText();
                    }
            }

            public void onFinish() {
                minuteTens = "0";
                minuteOnes = "0";
                secondTens = "0";
                secondOnes = "0";
                setCountdownText();
                timerStarted = false;
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(1000);
            }
        }.start();
        timerStarted = true;
    } else {
        if (timerPaused == false) {
            pauseTimer();
        } else {
            resumeTimer();
        }
    }
}

and also:

private void resumeTimer() {
    timerPaused = false;
    startTime = timeRemaining;
        CountDownTimer countDownTimer = new CountDownTimer(startTime, interval) {

            public void onTick(long millisUntilFinished) {
                if (timerPaused == true) {
                    cancel();
                } else {
                    timeRemaining = millisUntilFinished;
                    long remainder = millisUntilFinished;
                    minuteTens = df.format(millisUntilFinished / 600000);
                    remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
                    minuteOnes = df.format(remainder / 60000);
                    remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
                    secondTens = df.format(remainder / 10000);
                    remainder = remainder - (Integer.valueOf(secondTens) * 10000);
                    secondOnes = df.format(remainder / 1000);
                    setCountdownText();
                }
            }

            public void onFinish() {
                minuteTens = "0";
                minuteOnes = "0";
                secondTens = "0";
                secondOnes = "0";
                setCountdownText();
                timerStarted = false;
                Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(1000);
            }
        }.start();
    }

and the reset method:

private void resetScores() {
    final TextView P1CountDownTimer = (TextView) findViewById(R.id.P1CountDownTimer);
    final TextView P2CountDownTimer = (TextView) findViewById(R.id.P2CountDownTimer);
    timerStarted = false;
    timerPaused = false;
    minuteTens = "0";
    minuteOnes = "0";
    secondTens = "0";
    secondOnes = "0";
    if (countDownTimer != null) {
        countDownTimer.cancel();
        countDownTimer = null;
    }
    setCountdownText();
    P1CountDownTimer.setVisibility(View.INVISIBLE);
    P2CountDownTimer.setVisibility(View.INVISIBLE);
}

Edit:

This is also relevant. This is how I instantiate the timer in the first place, and then again after reset. Takes data from the user and converts it to milliseconds to pass into the countdowntimer:

private void resetTimer() {
    final TextView P1CountDownTimer = (TextView) findViewById(R.id.P1CountDownTimer);
    final TextView P2CountDownTimer = (TextView) findViewById(R.id.P2CountDownTimer);
    startTime = ((Tens * 60) + (Ones)) * 1000;
    timeRemaining = startTime;
    if (startTime > 0) {
        long remainder = startTime;
        minuteTens = df.format(startTime / 600000);
        remainder = remainder - (Integer.valueOf(minuteTens) * 600000);
        minuteOnes = df.format(remainder / 60000);
        remainder = remainder - (Integer.valueOf(minuteOnes) * 60000);
        secondTens = df.format(remainder / 10000);
        remainder = remainder - (Integer.valueOf(secondTens) * 10000);
        secondOnes = df.format(remainder / 1000);
        setCountdownText();
        P1CountDownTimer.setVisibility(View.VISIBLE);
        P2CountDownTimer.setVisibility(View.VISIBLE);
    }
}
Bisclavret
  • 1,327
  • 9
  • 37
  • 65

1 Answers1

1

It appears the way to make it work is to make your timer global and then call cancel on the global var.
See https://stackoverflow.com/a/27730328/1410671 and https://stackoverflow.com/a/23630959/1410671

I'm assuming these methods are all in your Activity class in which case you would declare your CountDownTimer at the top of the Activity class

private CountDownTimer countDownTimer;

and then refer to it later in the other methods like

countDownTimer = new CountDownTimer(startTime, interval) {...}
Community
  • 1
  • 1
randal4
  • 590
  • 4
  • 16