1

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.

2
  • 1
    "Canceling the old one doesn't make things any better." -- please edit your minimal reproducible example to show how you tried to do this. Off the cuff, as the first line in the onClick() lambda, if (::timer1.isInitialized) timer1.cancel() should work, as you will not have reassigned timer1 by that point. All that being said, it is unlikely that CountDownTimer is the correct solution for whatever problem it is that you are trying to solve. Commented Feb 12 at 22:42
  • It works! Thanks. Why do you think countdowntimer is not the right thing? Commented Feb 12 at 23:05

1 Answer 1

3

Replace your Button() with:

Button(onClick = {
               if (::timer1.isInitialized) timer1.cancel()

               timer1 = object: CountDownTimer(20000000, 1000) {
                           override fun onTick(millisUntilFinished: Long) {
                                    Log.d("mytagtt","timer")
                           }
    
                            override fun onFinish() {
                                    TODO("Not yet implemented")
                              }
                        }.start()
             }){ Text("starttimer") }

This is ugly — using .isInitialized is a "code smell" IMHO — but it is the simplest solution within the confines of the code in your question.

Why do you think countdowntimer is not the right thing?

Partly because it has never been the right solution IMHO. It was something that they tossed into Android 1.0 and have kept around for backwards compatibility reasons, but it never really worked all that well given other Android architecture aspects, such as configuration changes.

Partly because you wrote "The timer needs to run in background so launcheffect doesn't work for me", and CountDownTimer is not well-suited for many background scenarios.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.