87

I want to understand the concept of callback. I have searched on internet about the callbacks and there are many examples using interface, and one class is calling a method of another class using that interface. But still I can't get the main concept of callbacks, what is the purpose of using callbacks?

laalto
  • 150,114
  • 66
  • 286
  • 303
Hammad Shahid
  • 2,216
  • 5
  • 32
  • 60

6 Answers6

181

Here is a nice tutorial, which describes callbacks and the use-case well.

The concept of callbacks is to inform a class synchronous / asynchronous if some work in another class is done. Some call it the Hollywood principle: "Don't call us we call you".

Here's a example:

class A implements ICallback {
     MyObject o;
     B b = new B(this, someParameter);

     @Override
     public void callback(MyObject o){
           this.o = o;
     }
}

class B {
     ICallback ic;
     B(ICallback ic, someParameter){
         this.ic = ic;
     }

    new Thread(new Runnable(){
         public void run(){
             // some calculation
             ic.callback(myObject)
         }
    }).start(); 
}

interface ICallback{
    public void callback(MyObject o);
}

Class A calls Class B to get some work done in a Thread. If the Thread finished the work, it will inform Class A over the callback and provide the results. So there is no need for polling or something. You will get the results as soon as they are available.

In Android Callbacks are used f.e. between Activities and Fragments. Because Fragments should be modular you can define a callback in the Fragment to call methods in the Activity.

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • 3
    Thanks a lot , now I have an idea of callbacks – Hammad Shahid Aug 05 '13 at 09:37
  • Apologies for the necropost, but I think there is a small mistake in the example. I think the code for "new Thread(new Runnable()..." should be inside the class B's constructor. While trying to run your example, I was getting an error, but after putting the code in the constructor, it worked. Apologies if I am wrong here. – Tim Apr 19 '21 at 23:30
  • "In Android Callbacks are used f.e. between ..." What is "f.e."?? – nj2237 Jul 13 '21 at 17:30
  • 1
    @nj2237 f.e. is "for example" – AD Progress Aug 23 '21 at 09:13
50

You create an interface first, then define a method, which would act as a callback. In this example we would have two classes, one classA and another classB

Interface:

public interface OnCustomEventListener{
  public void onEvent();   //method, which can have parameters
}

the listener itself in classB (we only set the listener in classB)

private OnCustomEventListener mListener; //listener field

//setting the listener
public void setCustomEventListener(OnCustomEventListener eventListener) {
   this.mListener=eventListener;
}

in classA, how we start listening for whatever classB has to tell

classB.setCustomEventListener(new OnCustomEventListener(){
    public void onEvent(){
       //do whatever you want to do when the event is performed.
    }
});  

how do we trigger an event from classB (for example on button pressed)

if(this.mListener!=null){
   this.mListener.onEvent();
}

P.S. Your custom listener may have as many parameters as you want

Source

Community
  • 1
  • 1
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • This is pretty helpful - but I have a question...in the above case class B defines the listener, which would mean that any class that wants to listen to the event has to create a new object of B and then has to call the method. eg - from class A --> B b = new B(); b.setCustomEventListener(this); similarly from class D/class E B b = new B(); b.setCustomEventListener(this); Is there any way, I can avoid creation of object B in all the classes where I want to listen? – akash89 Jul 17 '20 at 06:56
9

Callback can be very helpful in Java.

Using Callback you can notify another Class of an asynchronous action that has completed with success or error.

Nargis
  • 4,687
  • 1
  • 28
  • 45
4

CallBack Interface are used for Fragment to Fragment communication in android.

Refer here for your understanding.

3

It was discussed before here.

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.

Community
  • 1
  • 1
2

I am using in the following case: In UI I got an action from a button, for eg. the user want to download an 500mb file. Thank I will initialize the background engine (AsyncTask class) and pass parameters to him. On the UI I will show a blocking progress dialog and disable the user to make any other clicks. The question is: when to remove the blocking from UI? the answer is: when the engine finished with success or fail, and that can be with callbacks or notifications.

What is the difference between notification and callbacks it is another question, but 1:1 is faster the callback.