什么是 Android 中的回调?

我想了解复试的概念。我在互联网上搜索了关于回调函数的信息,有很多使用接口的例子,其中一个类正在使用该接口调用另一个类的方法。但是我仍然不能得到回调的主要概念,使用回调的目的是什么?

166906 次浏览

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

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.

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.

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.

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.

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

Refer here for your understanding.