I have lots of buttons in my Activity, I have the following questions:
Should I create multiple inner anonymous classes for OnClickListeners for each button, like below:
private View.OnClickListener mShuffleListener = new View.OnClickListener() {
public void onClick(View v) {
/// task to do
}
};
Or should I go for a named inner class and add an if condition to check which click listener was called.
Which one is better to save memory resources?
Which one is cool to save mem resources?
It will make hardly any difference. At most 1 word ... and that is comparing a static inner class with a (non-static) anonymous class. A saving as small as that is not worth the code readability / maintainability penalty, even (IMO) if you've got a hundreds of these buttons.
There are three ways to handle event. Please have a look on the following link
http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android-java-application/
See the following to know the use of anonymous class and inner class
anonymous class
Use anonymous inner classes if you want the code not to be used anywhere else (this class is being used just here and nowhere else.)
inner class
inner class code can be used (if only by the class that created it if made private). If you see a named inner class, someone might think it'd be used in multiple places in the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With