Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In blackberry we can override keyChar() method and capture the keypress event or we can register onKeyPressListener. i know, onKeyListener is observer pattern.

In android also there is a KeyEvent.callback and onKeyListener

Both are events why we no need to register for keyChar event. what is the difference between both pattern?

share|improve this question
up vote 18 down vote accepted

Callback. The other code tells the developer: Hey, if this event occurs, I'll call the function in this bucket. You must know where the bucket is to connect your callback.

Listeners. The other code tells the developer: Hey, if this thing occurs, I'll send this event. You can connect your handler (hopefully) where it makes sense to you.

share|improve this answer
which one should i accept as answer? you and ammoQ both are right. Its a split pot. – Vivart Jun 17 '11 at 12:43
@Vivart - Easy, accept Andrea's, it provides more detail and ammoQ doesn't really need more rep. *8') – Mark Booth Jun 17 '11 at 14:38
It may also be relevant to say that callbacks are often passed to the action which would create the need for them. In many cases, the caller wouldn't "connect" a callback, but rather pass it to a method whose job is, e.g. "Request some data from a remote system, and invoke a caller-supplied method when a response is received or an error occurs". Outside code doesn't generally "connect" a callback; rather, the called code is responsible for establishing the connection to the passed-in method. – supercat Apr 6 '15 at 15:49

There can be many listeners for some type of event, but only one callback.

share|improve this answer
In android we can set only one OnClickListener to a view. – Vivart Jun 17 '11 at 8:28
It's quite possible that their definition of "listener" doesn't match that of other products. Usualy, you don't have a "setFooBarListener()" method, but an "addFooBarListener()" method. – user281377 Jun 17 '11 at 8:31
Another distinction could be that callback means a function (or closure or delegate), while listener is an object of a class that implements the corresponding Listener interface. – user281377 Jun 17 '11 at 8:42
Thanks @ammoQ and sorry i can accept only one answer. – Vivart Jun 17 '11 at 16:17
Vivart: doesn't matter, my answer isn't that good anyway – user281377 Jun 17 '11 at 17:17

I suppose that you have an application which use a library:

Listener: Library runs some code on its data, but, the code is implemented on Application.

Callback: Application runs some code on its data, however, the code is implemented on Library.

Because library can't see the application, so there is no way to instead Listener for that case.

Of course Application can see the library, but sometimes library wants to hide it's code, and helps application to make sense with it

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.