Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listener vs handler in android

What is the difference between a listener and handler?

I have searched a lot, but I couldn't find a suitable explanation. Where do I use a listener and where do I use a handler in Android?

I have gone through the following link as well:

Are event handler, event listener, and event registration all referring to the same thing?

Where can I get a comparative discussion of these two items? Also, can anyone tell me what are the different kinds of listener and handler available?

like image 992
scooby Avatar asked Jun 19 '12 12:06

scooby


1 Answers

The basic difference is that event handlers let the originating object itself do something in response to the event, whereas event listeners let other objects do something in response to events originating in the object.

For example: your activity has a button. If you want your activity to handle when someone touches the button, you use an event listener (by doing btn.setOnTouchListener(...)). BUT, if you want to create a specialized button (e.g. one that looks like a dog and barks when touched), you can create a subclass of Button and implement its event handler, onTouchEvent(...). In this latter case, the button itself will handle its touch event.

like image 176
Blah0x7B9 Avatar answered Oct 10 '22 17:10

Blah0x7B9