Android difference between callback and listener

Viewed 1774

So in my understanding listener is usually used to trigger some class when some thing happened on particular place. (for example when we click the button we want to do some action).

On the other hand I see some places where callbacks are used to execute/enqueue some code in different place. (retrofit uses this to enqueue API calls)

What is the main difference between these 2?

4 Answers

In short, they are the same.

Really?

Yes. But there are some "theoretical" (and in some cases practical differences).

Concepts

Listener is a word that in Android, it's commonly associated with a View ClickListener or similar. Where there are methods like addxxxListener(...) etc.

A Callback is often heard in the context of "for this particular event" I supply a "callback" and I will be called back when something happens.

In practice, they are often just instances of some interface passed along.

In my limited experience (only about 10 years of Java/Android -now Kotlin as well-), the term is used interchangeably.

I normally think of a callback when I am expecting something to "call me back" when something happens, and a listener as something where I listen to an event but as you can see by reading this... they could be the same. :)

It's often mentioned that one could have multiple listeners, but one callback (but there's nothing enforcing this and I have seen all use cases you can think of where either term is used) "You can add multiple callbacks" is not uncommon, even though if there are "multiple" then it "must be a listener". In fact, if you go as far as Android's View, the method is view.setOnClickListener { }

So it's a Listener, but you can only set ONE. So the above rule about 1 callback, N listeners is already broken since the early days of Android.

In some cases though, the convention is more tied to the method name, rather than the class' name:

setXXXyyy(...) allows you to set "one" and only one yyy listener/callback. addXXXyyy(...) allows you to add one or more (though not always, so read the documentation) yyy listener/callback.

An add is often (...but not always) accompanied by a removeXXXyyy(...) implying that if you keep/have a reference to a callback/listener you could remove it (and no longer be listening or be called back).

In the case of set, it's often expected that if needed you call set...(null) and pass that null to "remove" the sole listener/callback. (Views do this for you, you don't need to call it, but you can).

Anyway, don't quote me on this, but I'd argue they are the same. This is also more complicated when you involve other languages/frameworks/tools, where there's an even blurrier line.

In many instances, you will see callback used, but the method contains the word "Listener", and vice-verse, so don't get too crazy about it.

If you can, where you can, however, don't use either, just use Coroutines :)

For a concrete example of how both are used as the same thing...

Look at the Android View.setOnClickListener method:

    /**
     * Register a callback to be invoked when this view is clicked. If this view is not
     * clickable, it becomes clickable.
     *
     * @param l The callback that will run
     *
     * @see #setClickable(boolean)
     */
    public void setOnClickListener(@Nullable OnClickListener l) {

Notice something strange?

The method is called setOnClickListener yet the Javadocs say: "a callback..."

They are the same! Two ways of naming an interface. In general, Listener is also a callback and VICE VERSA!

for a type of event you can have many listeners , but one callback !

A callback is a procedure where you pass as an argument to another procedure. The procedure receiving the parameter can call it, or share it so some other procedures in the system can call it.

A listener watches for an event to be fired. For example, KeyListener waits for KeyEvents, a MessageListener waits for messages to arrive on a queue, and so on.

Related