Android 2.1: Muliple Handlers in One Action

I have more than one Handlers

in Activity. I am creating all the handlers in the onCreate()

main activity. My understanding of the method of handleMessage()

each handler will never be called at the same time, because all messages are placed on one queue (the MessageQueue activity thread). Therefore, they will be executed in the order in which they are placed in the queue. They will also execute on the main thread of the activity. It's right?

 public void onCreate() {

this.handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {

                            //operation 1 : some operation with instanceVariable1
super.handleMessage(msg);
}
};

this.handler2 = new Handler() {

@Override
public void handleMessage(Message msg) {
                            //Operation 2: some operation with instanceVariable1
super.handleMessage(msg);
}

};

this.handler3 = new Handler() {
@Override
public void handleMessage(Message msg) {
                            //Operation 3: some operation with instanceVariable1
super.handleMessage(msg);

}
};
}

      

+2


a source to share


2 answers


In the docs "When you create a new handler, it is bound to the thread / message queue of the thread that creates it - from now on, it will deliver messages and executables to that message queue and execute them as they leave the message queue."



So you are right, they will work in the order in which you queue them to the UI (since you are going to create them in onCreate).

+2


a source


One message at a time, per thread and per handler.

  • Each new Handler (...) instance is bound, explicitly or implicitly, to a Looper instance, and only once.
  • a Looper instance already created somewhere with a call to Looper.prepare () // usually gets Activity.getMainLooper () or Looper.myLooper ()
  • Looper.prepare () uses the ThreadLocal sThreadLocal variable (static field) to have one Looper instance per thread. (It works the same as hashMap.put (Thread.getCurrent (), new Looper ()))
  • each looper has its own MessageQueue
  • each Looper instance has its own main loop () method

    loop(){
        while(true){
            Message msg = messageQueue.next();
            msg.target.dispatchMessage(msg);
        }
    }
    
          

  • Each message has a target (handler) set and an exception is thrown (in MessageQueue.enqueueMessage ()) if it is not.

  • since a Handler cannot be bound to several Loopers, this means that each handler receives only one message at a time and only using msg.target == handler

so sendMessage () or postMessage () works something like this:



    handler.post(Message msg){
        Looper.sThreadLocal.get(Thread.getCurrent()).messageQueue.push(msg);
    }

      

so the call stack, while the message is with the message, should look something like this:

Looper.myLooper()-> Thread.getCurrent()-> Looper-> MessageQueue.next()-> Message-> Message.target-> Handler-> dispatchMessage()-> handleMessage()

      

+2


a source







All Articles