I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pa
Although the question doesn't include the Kotlin tag, I still think this answer belongs here due to Android tag.
I've made a very simple library that uses the CountDownTimer internally and inspired by this answer. So far, it works as intended with my very basic use case:
Pause the timer when the app/activity is paused, and resume when the app/activity resumes.
class GameActivity : Activity() {
private var timer = CustomTimer(30000, 1000) // 30 seconds duration at 1 second interval
override fun onCreate(savedInstanceState: Bundle?) {
...
timer.onTick = { millisUntilFinished ->
textView.text = "seconds remaining: " + millisUntilFinished / 1000
}
timer.onFinish = {
TODO("do something here")
}
}
override fun onResume() {
super.onResume()
timer.resume()
}
override fun onPause() {
super.onPause()
timer.pause()
}
}
Resumable CustomTimer.kt (50 lines)