Should we use anonymous classes for OnClickListeners or internal named classes?

I have many buttons in my work, I have the following questions:

  • Do I have to create several internal anonymous classes for OnClickListeners for each button, as shown below:

    private View.OnClickListener mShuffleListener = new View.OnClickListener() { public void onClick(View v) { /// task to do } }; 
  • Or I need to go for a named inner class and add an if condition to check which calling click was invoked.

Which one is better to conserve memory resources?

+7
source share
4 answers

Which one is cool to conserve memory resources?

This is unlikely to change. No more than 1 word ... and this is a comparison of a static inner class with a (non-static) anonymous class. The savings are so small that it’s not worth it for readability of the code / support, even (IMO) if you have hundreds of these buttons.

+7
source

I usually prefer a more subtle approach, which makes it easier to read code when using an onClick listener.

There is a property called onClick for almost all widgets in the properties menu (also available in the xml layout), there you can write the name of the Ex xyz method

now go to the java source file, there you just write the Ex method

 public void xyz(View v) { //your code goes here } 

and you are done. Also, if you really want to use inner classes, go anonymous if you are worried about your memory (each stored link takes up 8 bytes of memory in java if it is a reference type, which is in this case).

hope this helps ... ask if you need more clarification

+1
source

There are three ways to handle the event. Please view the following link.

http://tseng-blog.nge-web.net/blog/2009/02/14/implementing-listeners-in-your-android-java-application/

Note the use of an anonymous class and inner class

anonymous class

Use anonymous inner classes if you want code not to be used anywhere else (this class is used only here and nowhere.)

inner class

internal class code can be used (if only the class that created it, if made private). If you see a named inner class, someone might think it will be used in several places in the class.

+1
source

If all buttons have similar functionality, which differs only in a parameter that can be identified, it is better to create one listener and assign it to all buttons.

The location of the listener depends on the amount of variables that it needs to use. If he needs to use some method variables, he must be created inside the method, if he uses members of the class, he must be created inside the class.

For example, if you have ten buttons, each of which should start a different action, you can create a map of views and actions, and the corresponding action will begin in the listener's foundation:

 Map<View, Class<?>> viewActivityMap = new HashMap<View, Class<?>>(); // fill it somewhere // in onCreate View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Class<?> classToStart = viewActivityMap.get(v); Intent intent = new Intent(YourActivity.this, classToStart); startActivity(intent); } } button1.setOnClickListener(listener); button2.setOnClickListener(listener); button3.setOnClickListener(listener); 

In the case of listeners, the only real reason that I see the creation of an inner class is to create a constructor that receives parameters different from the implemented class / interface constructors.

+1
source

All Articles