0

all, i want to create multiple handlers that gets triggered based on users selection, but this handlers implement the same runnable method. the only difference is that they call different postDelayed() method. how do i go about achieving this without rewriting the same code for the runnable?

i am still not clear about how the handleMessages() works and if it can be used for this?.. thank you

DeRagan
  • 22,827
  • 6
  • 41
  • 50
irobotxx
  • 5,963
  • 11
  • 62
  • 91

1 Answers1

1

Use a handler and use a switch case around it. Update the view by sending a message to your handler

Handler Handlerobject;

Handlerobject= new Handler()
{
  public void handleMessage(Message msg) {

  switch(msg.what)
  {
   case 1:
  // Your code to update the UI

   break;

   case 2:
  // Your code to update the UI
   break;           
  }         
}};

Handlerobject.sendEmptyMessage(1) or sendEmptyMessageDelayed
DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • thanks for the response. from your code, does that mean that the postDelayed() method calls from the different handlers, comes in as messages or am i missing the concept? thanks once again – irobotxx Nov 02 '10 at 14:41
  • Handler is a light weighed method used to update the view. Once you are outside the main thread you need to use these methods to update the UI. Note that these methods run on the main thread so keep them light to increase the execution time. The above method uses a single Handler object but you can add in multiple cases to update the UI. You can pass a parameter in your sendEmptyMessage() to do that. – DeRagan Nov 02 '10 at 15:17