I want to post a runnable to a view object inside a runnable , currently i am stuck here.
var runnable = Runnable {
if(numLinesToDraw >= amplititudes.size){
}
else
{
numLinesToDraw = numLinesToDraw ++
invalidate()
postDelayed({
},2000)
}
}
postDelayed(runnable,2000)
As you can see , there is a postDelayed
method inside the runnable. What i want to do is post the same runnable again and so on. What should i add here?
postDelayed({
},2000)
In Kotlin 1.2+, you can define a local lateinit var for the runnable and then ititialize it with a Runnable that uses the variable: lateinit var runnable: Runnable runnable = Runnable { /* ... */ postDelayed (runnable,2000) }
The first method will immediately push the runnable inside the MessageQueue of the main thread. 2nd method will hand the runnable at fixed time and 3rd method will delay the posting by the time given as second parameter of the 3rd method.
Yes, we do need a runnable. Suppose, you need to execute 2 different tasks in two different steps of a program using thread. Now you can pass these runnable as parameters to execute on any background thread at any time. Handler is the most precious thing on android framework. Yes it is not a java thing.
Kotlin Handler - How do i stop Handler in Android Published April 21, 2022 In this android example we will cover what is Handler and how to stop Handler in Android application with kotlin code. Handler is a Thread class which will used to send and manage the Message and Runnunable objects.
In Kotlin 1.2+, you can define a local lateinit var
for the runnable
and then ititialize it with a Runnable
that uses the variable:
lateinit var runnable: Runnable
runnable = Runnable {
/* ... */
postDelayed(runnable,2000)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With