AAnkit
AAnkit

Reputation: 27549

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

I have lots of buttons in my Activity, I have the following questions:

  1. 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
        }
    };
    
  2. 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?

Upvotes: 7

Views: 4404

Answers (4)

Stephen C
Stephen C

Reputation: 718826

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.

Upvotes: 7

MByD
MByD

Reputation: 137332

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

The location of the listener is dependent of the scope of variables it need to use. If it needs to use some method variables, it should be created inside the method, if it uses a class members, it should be created inside the class.

For example, if you have ten buttons that each should start a different activity, you can create a map of views and activities, and in the listener fund the appropriate activity to start:

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 case of listeners, the only real reason I see to create an inner class, is if you want to create a constructor that receives parameters that are different from the implemented class/interface constructors.

Upvotes: 1

Nav
Nav

Reputation: 10422

I generally prefer a more subtle approach which makes the code easier to read when using onClick listener

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

now go to your java source file there you simply write a method Ex

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

and you are done .Also if you really wanna use inner classes then go with anonymous ones if memory is you concern(each reference stored takes 8 bytes of memory in java if its a reference type which in this case it is).

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

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

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.

Upvotes: 2

Related Questions