It is more efficient to create one Handler that takes care of a bunch of events, than separate inner class Runnables for each of them. So I would suggest something like this:
static final int MSG1 = 1; static final int MSG2 = 2; void do1() { } void do2() { } final Handler mHandler = new Handler() { void handleMessage(Message m) { switch (m.what) { case MSG1: do1(); break; case MSG2: do2(); break; } } } In the examples below, if the Handler is only going to do one thing, then there is no difference really between it and a Runnable, so I would suggest using a Runnable. But ideally you would do something like the above, a Handler that does multiple things. And you can structure that however you want -- as an anonymous inner class like this, as a regular inner class, as a separate class, etc. On Mar 24, 2:58 am, "Harsh Jain" <[EMAIL PROTECTED]> wrote: > w.r.t andoid which of the two is better.. > a) Implement runnables as inner classes and post it to a handler. Note here > variables can be directly accessed. > > class AService { > String x, y, z; // packaged scope for efficient access. > > class B implements Runnable { > void run() { > x = y + z ;// do something with x,y,z > } > } > > onCreate() { > mWorker.handler.postRunnable(new B()); > } > > } > > b) Create a MyHandler extending handler and receive messages and translate > them to the appropriate action. > > class MyHandler extends Handler { > AService a; > public MyHandler(AService a) { } > > void action1() { > a.x = a.y + a.z // Do something with a's inner variables. > } > > void handleMessage(Message w) { > action1(); > } > > } > > I guess people know what i mean. > > regards, > harsh --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] Announcing the new M5 SDK! http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---