Mohit Kumar
Mohit Kumar

Reputation: 732

Handler() is deprecated

I am trying to update a seekbar every second in background using Kotlin & Coroutines but It says Handler() is deprecated use it with looper and i am confused how to use handler, looper and kotlin coroutines

The code i tried to make it work

val myHandler: Handler = Handler()
myHandler.postAtTime(asyncSeekBar(), 1000)

Here showing Handler() is deprecated

asyncSeekBar()

private fun asyncsSeekBar() {
        CoroutineScope(Default).launch {
            updateSeekBar()
        }
    }

but asyncSeekBar should be Runnable therefore showing and error

Type mismatch. Required: Runnable Found: Unit

If there is any other way of doing it with more simpler way, please suggest

Upvotes: 0

Views: 967

Answers (2)

Animesh Sahu
Animesh Sahu

Reputation: 8096

Why are you using Handler when you have coroutines?

scope.launch {
    delay(1000)  // 1000ms
    asyncSeekBar()
}

The error you're getting is because you're not passing an implementation of Runnable interface, you are instead calling the function and returning the return value which is nothing (Unit) to the postAtTime().

Upvotes: 1

Saurabh Thorat
Saurabh Thorat

Reputation: 20684

You can use LifecycleScope for this to run every second:

lifecycleScope.launch {
    while (true) {
        delay(1000)
        updateSeekBar()
    }
}

Upvotes: 1

Related Questions