July 2018
Intermediate to advanced
266 pages
7h 11m
English
Android's UI dispatcher has a rather interesting implementation. First's there's a HandlerContext class that takes an android.os.Handler and a name. And the dispatch function simply forwards the Runnable to the post() function of the handler:
public class HandlerContext( private val handler: Handler, private val name: String? = null) : CoroutineDispatcher(), Delay { override fun dispatch(context: CoroutineContext, block: Runnable) { handler.post(block) }}
Then, an instance of it called UI is created. It passes the main looper and the name UI to the constructor:
val UI = HandlerContext(Handler(Looper.getMainLooper()), "UI")
Read now
Unlock full access