I use this fragment of code in an activity to display a timer countdown:
//SET COUNTDOWN TIMER in onCreate()
//var tempo is a :Long number
var textView = findViewById<TextView>(R.id.countdownTimer)
object : CountDownTimer(tempo, 1000) {
override fun onTick(millisUntilFinished: Long) {
textView.setText("Remaining time: "+tim(millisUntilFinished) )
}
override fun onFinish() {
textView.text = "Times up!"
}
}.start()
If possible I'd like to implement the possibility to pause this CountDownTimer in onBackPressed() and resume it again?
override fun onBackPressed() {
AlertDialog.Builder(this).apply {
setTitle("Confirm")
setMessage("Exit app?")
setPositiveButton("Yes") { _, _ ->
// if user press yes, cancel timer?
super.onBackPressed()
}
setNegativeButton("No"){_, _ ->
// if user press no, then return the activity and resume timer?
}
setCancelable(true)
}.create().show()
}
My idea would be to edit var tempo differentitate it everytime OnBackPressed and copy the section of this 1st code piece in onCreate() and put it onResume().
I'm quite confused on using this tbh. MAybe there's a faster and better way?