I have a countdown timer and I want to add 10 seconds to it whenever my isAddTime method is true. I have successfully added it but whenever the time is added, it still finishes on the current time.
I am cancelling it in OnTick Method i tried to cancel it in other class and i successfully cancel it but i cant resume the timer.. since to cancel it in another class it will be static.
Here's my code:
timerHolder = new CountDownTimer(30000, 1000) {
@Override
public void onFinish() {
Class.isAddTime = false;
Intent i = new Intent(getBaseContext(), OtherClass.class);
startActivity(i);
}
@Override
public void onTick(long millisUntilFinished) {
timeLeft = millisUntilFinished;
if(Class.isAddTime()){
timerHolder.cancel();
AddTime();
Class.isAddTime = false;
}
timer.setText("Time left: " + String.valueOf(timeLeft / 1000));
}
}.start();
And here is the AddTime():
private void AddTime(){
timerHolder = new CountDownTimer(timeLeft + 10000, 1000) {
@Override
public void onFinish() {
Class.isAddTime = false;
Intent i = new Intent(getBaseContext(), OtherClass.class);
startActivity(i);
}
@Override
public void onTick(long millisUntilFinished) {
timeLeft += 10000;
timeLeft = millisUntilFinished;
if(Class.isAddTime()){
timerHolder.cancel();
AddTime();
Class.isAddTime = false;
}
timer.setText("Time left: " + String.valueOf(timeLeft / 1000));
}
}.start();
I was wondering why it still continue the current time even if i had it cancelled? is there other way to call cancel? is there more clean solution on how i can add time to the countdown timer?