0

Hi I am making simple countdown timer and I would like to resume it after pause. Very important for me is that I won't put my countdown timer to public void method.

   mTextViewCountDown = findViewById(R.id.text_view_countdown);
    mButtonStartPause = findViewById(R.id.button_start_pause);
    mButtonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mTimerRunning) {
                pauseTimer();
            } else {
                startTimer();
            }
        }
    });
   updateCountDownText();
    mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateCountDownText();
        }
        @Override
        public void onFinish() {
           mTimerRunning = false;
        }
    }.start();
}
private void startTimer() {

    mTimerRunning = true;
    mCountDownTimer.start();
}
private void pauseTimer() {
    mCountDownTimer.cancel();
    mTimerRunning = false;
    mButtonStartPause.setText("Start");
}
private void updateCountDownText() {
    int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
    mTextViewCountDown.setText(timeLeftFormatted);
}
Jezdi78
  • 21
  • 4
  • Why is it important not to use a public void method? I'd use a public boolean method for restarting anyway. – NomadMaker Nov 27 '20 at 10:23
  • Because I would like to make another countdown where Is so many variables and It's isn't possible. I tried it – Jezdi78 Nov 27 '20 at 10:36
  • I don't understand, but that isn't necessary. I do find it interesting that you still have questions on the basic timer, and yet you **know** that something more complex is **impossible**. I wish I could help you, but I'm not willing to tackle the impossible. – NomadMaker Nov 27 '20 at 10:40
  • Because I am new in android studio and I am making sport countdown timer. my code has 200 line and I always making simple things and when its working I put this to my main countdown – Jezdi78 Nov 29 '20 at 12:04

1 Answers1

0

it is but you need to make a new instance of the timer and passing the current paused time

read the details here : Pausing/stopping and starting/resuming Java TimerTask continuously?