Programming convention on Anonymous Class vs Implementing Interface

android, anonymous-class, interface, java

Solution

I think 2nd approch is good as

1- you can handle multiple Views click at one place...

2- it make code shorter and easy to read..

3- it is easy in maintenance.

4- if you are using the Base Activity like concept in your project then it is also useful.

Problem

From the android development perspective, while you are programming which way do you prefer to implement for listener? Or which way do you think is the best for readable code? I gave two example about these things but think more complex classes such as which has more than one Listener:) First example which is an Anonymous Class: ``` public class SenderReceiverActivity extends Activity { Button cancelButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sending); cancelButton = (Button) findViewById(R.id.button1); cancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { } }); }} ``` Second example which is implementing interface : ``` public class SenderReceiverActivity extends Activity implements OnClickListener { Button cancelButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sending); cancelButton = (Button) findViewById(R.id.button1); cancelButton.setOnClickListener(this); } public void onClick(View v) { } } ```

Original source