GO VEGAN
GO VEGAN

Reputation: 1143

CountDownTimer in android - how to restart it

I have to restart a CountDownTimer. I read a lot of question here but no one of the answer helped me. When I use the following code

if(Const.counter != null){
    Const.counter.cancel();
    Const.counter = null;
}


Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000);
Const.counter.start();

I started a new counter but the old one also continues work. Please help me solve it.

Upvotes: 21

Views: 34146

Answers (5)

user13908049
user13908049

Reputation: 11

private fun startTimer() {
    var timeInMilliSeconds = 11000L
    val countDownTimer: CountDownTimer = object : CountDownTimer(timeInMilliSeconds, 1000) {
        override fun onFinish() {
            Timber.d("Times Up!")
            setupResult("")
            this.cancel()
            timeInMilliSeconds = 11000L
            this.start()
        }

        override fun onTick(p0: Long) {
            val seconds = (p0 / 1000) % 60
            Timber.d("Timer: $p0")
            timer?.text = "$seconds"
        }
    }
    countDownTimer.start()
}

Upvotes: 1

Daniel Espinosa
Daniel Espinosa

Reputation: 81

Just call again the start() method:

CountDownTimer cdt = new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        this.start(); //start again the CountDownTimer
    }
};

Upvotes: 2

Tejinder2202
Tejinder2202

Reputation: 11

coutdown timer for quiz

 if(countDownTimer!=null)
            {
                countDownTimer.cancel();
                countDownTimer.start();
                }
            else {
               countDownTimer = new CountDownTimer(30000, 1000) {

                    public void onTick(long l) {
                        mtimer.setText("remaining time" + l / 1000);//mtime is a textview
                    }

                    public void onFinish() {//here mnext is the button from which we can get next question.
                        mnext.performClick();//this is used to perform clik automatically

                    }
                }.start();

Upvotes: 1

WASEEM
WASEEM

Reputation: 187

I did some different trick here. Hope this will help you.

if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
        myCountDownTimer = new MyCountDownTimer(10000, 500);
        myCountDownTimer.start();

Upvotes: 8

Lazy Ninja
Lazy Ninja

Reputation: 22527

You can realize it by cancelling and restarting. The following example should work.

CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {}

    @Override
    public void onFinish() {
        isCounterRunning = false;
    }
};


boolean isCounterRunning  = false;

private void yourOperation() {
    if( !isCounterRunning ){
        isCounterRunning = true;
        mCountDownTimer.start();
    }
    else{
        mCountDownTimer.cancel(); // cancel
        mCountDownTimer.start();  // then restart
    }

}

Upvotes: 15

Related Questions