Pause CountDownTimer in Android when activity is not in front

后端 未结 7 602
旧巷少年郎
旧巷少年郎 2020-12-02 20:43

I have an activity that uses a CountDownTimer that counts down from 10. How do I pause that timer when the activity is no longer in focus, like if the user get a call or som

7条回答
  •  Happy的楠姐
    2020-12-02 21:18

    I would add something to the onTick handler to save the progress of the timer in your class (number of milliseconds left).

    In the onPause() method for the activity call cancel() on the timer.

    In the onResume() method for the activity create a new timer with the saved number of milliseconds left.

提交回复
热议问题