Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin, Android - How do i post the same runnable inside the runnable?

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)
like image 489
Dishonered Avatar asked May 29 '18 11:05

Dishonered


People also ask

How to create a runnable from a variable in Kotlin?

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) }

How to post a runnable in a thread in Java?

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.

Do we need a runnable in Android?

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.

What is handler in Android with Kotlin?

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.


1 Answers

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)
}
like image 191
hotkey Avatar answered Oct 15 '22 21:10

hotkey