Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Using Handlers?

Is there any problem with using multiple Handlers in the same Activity.

I noticed that in all samples provided in android official website they use a single handler and detect different actions depending on the value of "what", is this because of memory management, and high amount of memory used by the Handler? Or should I call it "bad code" and do it the clean way (Multiple handlers each responsible for a specific task)

Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
      if (msg.what == 0){
         // do something
      }
      else if (msg.what == 1){
         // do something else
      }
    }
}

OR

Handler taskHandlerA = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
         // do something
    }
}

Handler taskHandlerB = new Handler()
{
    @Override
    public void handleMessage(Message msg) {
         // do something else
    }
}
like image 573
aryaxt Avatar asked Jun 01 '11 02:06

aryaxt


1 Answers

No there isn't such a limit (a Handler is just a message receiver), but if you want to do such a thing the more common approach is to have one Handler that you post Runnable objects to.

like image 115
hackbod Avatar answered Oct 20 '22 10:10

hackbod