Reputation: 1532
Maybe stupid question, but I can't understand...
Is there a difference between the following ways?
I mean waste memory or something?
class A implements Handler.Callback {
// is it OK to pass the loopback reference into the Handler in this case?
Handler mHandler = new Handler(this);
public boolean handleMessage(Message msg) {
...
}
}
class B {
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
...
}
};
}
Thank you!
Upvotes: 0
Views: 235
Reputation: 7820
I would say it depends on your desired usage. If you have just one button and need to handle its click, I would use an anonymous class (like in your class B
). But if a view contains lots of buttons or other things, you want to handle click events for, I would use the first method with one single handler for all controls. This saves you creating an extra object for each control (coding and memory overheap), too!
I am using this way in a project currently, having my activity handling every click and then doing the further work:
public class MyActivity extends Activity implements View.OnClickListener {
...
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// handle button 1
break;
case R.id.button2:
// handle button 2
break;
// ...
}
}
...
}
Upvotes: 1