What is callback in Android?

I want to understand the concept of callback. I searched the Internet for callbacks, and there are many examples using the interface, and one class calls the method of another class using this interface. But still I cannot get the basic concept of callbacks, what is the purpose of using callbacks?

+50
java android
Aug 05 '13 at 9:27 am
source share
6 answers

Here is a good tutorial that describes callbacks and a use case.

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

Here is an 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 do some work on the thread. If Thread completes, it will inform Class A of the callback and provide the results. Therefore, there is no need for a survey or something like that. You will get the results as soon as they are available.

On Android, callbacks are used by fe between actions and fragments. Since fragments must be modular, you can define a callback in a fragment to call methods in an Activity.

+114
Aug 05 '13 at 9:34 on
source share

First you create an interface, and then you define a method that will 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 in class B (we install only the listener in class B)

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

in class A, how do we start listening to what class B should say

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

how do we fire an event from class B (for example, clicking a button)

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

PS Your custom listener can have as many parameters as you want.

Source

+32
Aug 05 '13 at 9:30 a.m.
source share

Callback can be very useful in Java.

Using the callback, you can notify another class of an asynchronous action that completed with success or error.

+6
Aug 05 '13 at 9:30 a.m.
source share

This has been discussed up to here .

In computer programming, a callback is part of an executable code that is passed as an argument to another code that is expected to call (execute) the argument at some convenient time. The call may be immediate, as with a synchronous callback, or may occur at a later time, as with an asynchronous callback.

+2
Aug 05 '13 at 9:31
source share

CallBack Interface are used to communicate Fragment - Fragment in android.

Contact here for your understanding.

+2
Aug 05 '13 at 9:51
source share

I use in the following case: In the user interface, I got an action from a button, for example. user wants to upload a 500 MB file. Thank you, I initialize the background engine (AsyncTask class) and pass parameters to it. In the user interface, I will show the lock dialog and disable the user from making other clicks. The question is when to remove a lock from the user interface? answer: when the engine finished successfully or unsuccessfully, and this can be with callbacks or notifications.

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

+1
Aug 05 '13 at 9:34 on
source share



All Articles