0

I usually implement click event bound to a certain visual element, like

final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });

Often I see examples where they use Activity-wide onclick (implementing View.OnClickListener) and then they do not create View.OnClickListener for each element but rather just pass this, like

public class MyClass extends Activity implements View.OnClickListener { 

    //...

    someUIElement.setOnClickListener(this);

    public void onClick(View view) {
       //TODO implement this
    }
}

When should I use such Activity-wide OnClick events? Are both ways the same

sandalone
  • 41,141
  • 63
  • 222
  • 338