marjanbaz
marjanbaz

Reputation: 1042

How to reset CountDownTimer on activity resume?

I don't use constant value for my countdown timer, cause it's different time for every next level.So I use timeCount variable set at 150000 for the first level:

    long timeCount = 150000;

public String formatTime(long millis) {  
                    String output = "00:00";  
                    long seconds = millis / 1000;  
                    long minutes = seconds / 60;  

                    seconds = seconds % 60;  
                    minutes = minutes % 60;  

                    String sec = String.valueOf(seconds);  
                    String min = String.valueOf(minutes);  

                    if (seconds < 10)  
                        sec = "0" + seconds;  
                    if (minutes < 10)  
                        min= "0" + minutes;  

                    output = min + " : " + sec;  
                    return output;
                }

And my timer:

public class MyCount extends CountDownTimer {
         public MyCount(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);
         }

         public void onFinish() {


         }
         public void onTick(long millisUntilFinished) {
             vreme.setText("" + millisUntilFinished / 1000);
         }
         }
    // New timer for 40 minutes, starts after initialization
   MyCount brojacVremena = new MyCount(timeCount, 1000) 
   {
       // Updates the text on your "scoreboard" every second
       public void onTick(long millisUntilFinished) 
       {
           vreme.setText("" + formatTime(millisUntilFinished));
       }

       public void onFinish() 
       {
        finish();
       }
   };

So, when the game ends, i present a popup window and the next level is up after OK is pressed. I call the popup and set new values, for time and some other stuff:

Intent i = new Intent(Kviz.this, Popup_nivoi.class);
            if(level==2){
                numberOfQuestions = 13;
                timeCount = 160000;
                greska = 7;
                level++;
}
brojacVremena.cancel();
            startActivityForResult(i, MY_REQUEST2);

But my countdowntimer always starts from the old time, 150000ms. How to reset it, cause me changing the timeCount value does not do the job. I don't know why. That variable is changed, I know that for sure, but countdown timer does not use it.

Upvotes: 0

Views: 1738

Answers (1)

Desert
Desert

Reputation: 2303

You can not reset time in already created CountDownTimer it is immutable object and your timeCount varibale is used only once when creating this object. So what you need to do is just create another timer with new time.

UPD: Here is some code

public class MyCount extends CountDownTimer {
     public MyCount(long millisInFuture, long countDownInterval) {
         super(millisInFuture, countDownInterval);
     }

     public void onFinish() {
         finish();
     }

     public void onTick(long millisUntilFinished) {
         vreme.setText("" + formatTime(millisUntilFinished));
     }
}
MyCount brojacVremena = new MyCount(timeCount, 1000);

Then when you need new timer you have to just write

brojacVremena = new MyCount(timeCount, 1000);

with new timeCount variable.

Upvotes: 2

Related Questions