Handler post is not working in Kotlin Android
You are not starting runnbale
. Try this:
mHandler!!.post { runnable.run() }
This is also valid:
mHandler!!.post {
Log.d("TEST", "++++ runable")
Log.d("TEST", "++++ come end")
}
Try this code, I hope this is working
Handler().postDelayed({
// You code and delay time
}, 1000L)
First at all, don't use !!
operator, it is a very bad practice (from the doc). With ?
you will reach the same behaviour but checking if the instance became null before executing it.
Saying this, using:
mHandler?.post { runnable }
You are actually creating a new lambda containing runnable line. see here below in a more readable way:
mHandler?.post {
runnable
}
This is the equivalent in Java:
mHandler.post(new Runnable(){
public void run(){
runnable;
}
});
To solve this:
Option 1: getting rid of the runnable declaration
mHandler?.post { /*the content of your runnable*/ }
Option 2: using your runnable instance
mHandler?.post(runnable) // normal parentheses
Option 3: crazy way
mHandler?.post { runnable.run() }