I'm relatively new to the "Looper-Handler" term. I'm pretty clear with the way they work but still confused with their architecture. I've also gone through couple of SO questions, but still I have some questions regarding them.
To get them clear and to summarize, can anyone answer for the below questions?
- Is it true that the
Handlerbelong to the thread in which it was created ? - I'm aware that there can be multiple instances of handler per
Looper/MeesageQueuepair. Is it possible create ahandlerfor a thread from some other Thread? - Suppose, in the main thread we write
Handler handler = new Handler (x.getLooper())where x is thehandlerof another thread X, then to which MessageQueuehandlerwill actually post?
If I'm getting wrong anywhere, please make me correct. Thanks.
val h = Handler(). Default constructor associates this handler with theLooperfor the current thread. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.Looperval h = Handler(looper), wherelooperassociated with anotherThread.handlerwill be associated withThreadX, because looper of that thread is used to created thehandler.The 3rd question is similar to the 2nd one.