2

I've a CountDownTimer that runs in a fragment normally. When I try to rotate the phone i don't visualize the count down on the timerView TextView.

This is the code of CountDownTimer

 private void startTimer(int min, int sec) {

        countDownTimer = new CountDownTimer(min, sec) {

            @Override
            public void onTick(long millisUntilFinished) {
                timerView.setText("" + String.format("%d min, %d sec",
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                mills = millisUntilFinished;


            }

            @Override
            public void onFinish() {
                Toast.makeText(mContext, "TIME OVER", Toast.LENGTH_SHORT).show();

            }
        }.start();

    }

this is the code in onPause callback

    @Override
    public void onPause() {
        Log.i(TAG, getClass().getSimpleName() + ":onPause()");

        super.onPause();
        countDownTimer.cancel();

    }

and this is the code in onResume CallBack

@Override
    public void onResume() {
        super.onResume();
        if(countDownTimer!=null)
            startTimer(mills,sec);
        Log.i(TAG, getClass().getSimpleName() + ":onResume()");



    }

In this way I cancel the count down in onPause event and start a new count down with remaining milliseconds in onResume event. I noticed using a log that after onResume the onTick callback is working but the view is not updated. I tried with invalidate but without any good result. What is the right way to manage the countdowntimer in a fragment?

adev
  • 367
  • 1
  • 3
  • 20

0 Answers0