java - Android handler handleMessage() method call -


when instantiate handler object (for example lets using anonymous inner class) in main ui thread.

handler h = new handler () {     @override     public void handlemessage (message msg) {         /* code handle message */     } }; 

we can pass handler object reference other thread can post status updates ui thread using reference (h.sendmessage()).

doubt: have not provided our custom anonymous class reference h handler anywhere activity class or ui thread. in order call overridden method ui thread must have object reference our class. how overridden handlemessage() called instead of default one?

your handlemessage() being called looper. can see here looper calls dispatchmessage on message target handler. , target being assigned this reference when post message inside handler. flow follows:

  1. you post message on handler, internally creates message object reference handler.

  2. that message put message queue managed main ui thred (in particular case)

  3. when time comes, android removing message queue , using target field - calls handlemesage on it.

also - whats important if dont specify particular looper when creating handler, default looper used thread being instantiated. in case main ui looper.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -