Chris Jones
Chris Jones

Reputation: 672

What is the difference between these two ways of using buttons and an OnClickListener?

I have a pretty crappy book so I am not sure for their reasoning for using either form.

The first way that it would go about it would be like this

 Button button = (Button) findViewById(R.id.btnButton)

 button.setOnClickListener(new OnClickListener() {
//code code code }

Then our book wants to use Global Variables instead of local, so it randomly starts using a different way to go about things

Button btButton;
//Done as Global Variable.

btButton = (Button) findViewById(R.id.btnButton);
btButton.setOnClickListener(bButton);


Button.OnClickListener bButton = new Button.OnClickListener(){
//code }

It is things like this that my book is horrible at and one reason why I will not be buying the next installment of it. I hate when it just changes the way it does things without any real reason. So can anyone really tell me the difference here? Thanks.

Upvotes: 1

Views: 144

Answers (6)

Ahmad
Ahmad

Reputation: 2190

button.setOnClickListener(new OnClickListener() 

Assume it have created a listener object on the fly and passed it to your setOnClickListener. This style is known in java as Anonymous Inner Classes. and second one is straightforward.

Button.OnClickListener bButton = new Button.OnClickListener(){

The reason why used Button.OnClickListener is that OnClickListener is in View Class. and Button Class inherits it from View. So to avoid import statement , we used directly Button.OnClickListener.

Upvotes: 2

Dalmas
Dalmas

Reputation: 26547

Technically, when you write :

button.setOnClickListener(new OnClickListener() { ... });

It will be converted to the following code at compile time :

Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... };
button.setOnClickListener(anonymous_listener);

The second example you provided is bad. It stores a useless reference to a OnClickListener which will never be used somewhere else than in your onCreate() method. It's only 8 bytes lost or so, but still it's useless and should be defined as a local variable.

Upvotes: 2

edthethird
edthethird

Reputation: 6263

As everyone says, there is no difference-- but nobody has explained why.

Button extends View.

When you are using onClickListener, you are using the one defined in View. Almost always, this one will work-- it won't with Dialogs, and maybe some other classes.

If you explicitly say Button.onClickListener, you are referencing the onClickListener defined in the Button class, and since Button extends View, this is the same as using the generic onClickListener.

There is also the inner anonymous vs explicit, but this is more of a preference. You will get better performance using a static onClickListener than in inner anonymous, simply because there is less garbage collection necessary, but you won't notice it unless you have 30 or so anonymous inner listeners.

Upvotes: 3

Sagar
Sagar

Reputation: 1335

Both are same.. First one uses anonymous inner class, and secon on creates an object of listner and pass it to method..

Second method comes with advantag of reusabbility of same listner instance.. but first on is preferred as generally we have different listners for every buttons

Upvotes: 4

Jug6ernaut
Jug6ernaut

Reputation: 8325

There is no real difference between the two examples you posted. Your book probably did not cover this because this is more of a Java question then Android. The first one your just dynamically declaring your OnClickListener instance, where on the second you declared it stataically and then referenced it. In execution they will behave exactly the same.

You can use the second example if for example you have more then one button. You can pass your OnClickListener to each and then process which button is pressed from within the OnClickListener instead of having a separate OnClickListener for each button.

Upvotes: 2

biddulph.r
biddulph.r

Reputation: 5266

You have nothing to worry about. They both do the same thing. In the first example you are creating the instance of OnClickListener when you set it to the button.

In the second case, the onClickListener has already been created beforehand, and you are passing in a reference to it.

Different people prefer to do it different ways, and they lend themselves to different functionality.

Obviously by setting it equal to bButton in the second example allows you to manipulate it later or use it in other button listeners, whereas you don't have a copy of the first one to do that with.

So if you wanted to use the exact same code for several buttons, the second option would allow you to add the listener to all the buttons.

Hope that seems a little clearer now

Upvotes: 2

Related Questions