The problem: if I smash the button, I ended up with multiple timers.
lateinit var timer1: CountDownTimer
...
Button(onClick = {
timer1 = object: CountDownTimer(20000000, 1000) {
override fun onTick(millisUntilFinished: Long) {
Log.d("mytagtt","timer")
}
override fun onFinish() {
TODO("Not yet implemented")
}
}.start()
}){ Text("starttimer") }
Canceling the old one doesn't make things any better. I think the problem is, that the object is no more assigned to the variable and therefor the timer doesn't respond to timer1.cancel().
How can I solve this problem. The timer needs to run in background so launcheffect doesn't work for me.
onClick()lambda,if (::timer1.isInitialized) timer1.cancel()should work, as you will not have reassignedtimer1by that point. All that being said, it is unlikely thatCountDownTimeris the correct solution for whatever problem it is that you are trying to solve.