Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implements OnClickListener VS. new Button.OnClickListener() {};

Tags:

java

android

I have a question about implementing OnClickListeners for developing with the ADT. I'm unsure of which way is more efficient, can anyone please provide me with pro's and con's of each approach?

class x extends Activity implements OnClickListener
{
  button.SetOnClickListener(this);
  OnclickListener(View v)
  {
    switch(v.getGetId());
    {
      case R.id.y:
      //do stuff here
      break;
      .
      .
      .
    }
  }

}

<-VERSUS->

class a extends Activity
{
   .
   .
   .
   btn.setOnClickListener(new Button.OnClickListener()
   {

    OnClickListener(View v)
    {
      //do stuff here
    }

   });

}
like image 514
jr3 Avatar asked Aug 03 '10 00:08

jr3


1 Answers

I think its mostly a case of personal preference. Any performance difference is likely going to be negligible.

Personally, I prefer the nested class:

  1. Its harder to screw up
  2. Switch statements are ugly
  3. You can make use of local variables that may be useful

But some people think that nested classes are ugly, and so prefer the implements approach. That approach does work better if you only have one listener implemented in the activity.

like image 158
Cheryl Simon Avatar answered Sep 22 '22 13:09

Cheryl Simon