android-ktx
android-ktx copied to clipboard
Looper.getMainLooper()-around extensions
I'm not sure that this is a good idea, cause we'd have a new instance of Handler each time this function is called, but would it worth to have something like this:
inline fun postOnUiThread(
crossinline action: () -> Unit
): Runnable {
val runnable = Runnable { action() }
Handler(Looper.getMainLooper()).post(runnable)
return runnable
}
And also the following boolean might be useful in some cases:
inline val isUiThread: Boolean
get() = Looper.getMainLooper().thread == Thread.currentThread()
Just as a reference to anko, they keep a reference of handler to avoid creating it each time. They will also avoid posting a runnable if you are already on the main thread