Java Android中的回调是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18054720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is callback in Android?
提问by Hammad Shahid
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?
我想了解回调的概念。我在互联网上搜索了有关回调的信息,并且有很多使用接口的示例,一个类正在使用该接口调用另一个类的方法。但是还是没搞懂回调的主要概念,使用回调的目的是什么?
采纳答案by Steve Benett
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.
A 类调用 B 类以在线程中完成一些工作。如果 Thread 完成工作,它将通过回调通知 Class A 并提供结果。所以不需要轮询什么的。您将在结果可用后立即获得结果。
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.
在 Android 回调中,在活动和片段之间使用 fe。因为 Fragment 应该是模块化的,所以您可以在 Fragment 中定义一个回调来调用 Activity 中的方法。
回答by Boris Mocialov
You create an interface first, then define a method, which would act as a callback. In this example we would have two classes, one classAand another classB
您首先创建一个接口,然后定义一个方法,该方法将充当回调。在此示例中,我们将有两个类,一个classA和另一个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)
classB 中的监听器本身(我们只在 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
在 classA 中,我们如何开始聆听 classB 必须告诉的任何内容
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)
我们如何从 classB 触发事件(例如按下按钮)
if(this.mListener!=null){
this.mListener.onEvent();
}
P.S. Your custom listener may have as many parameters as you want
PS您的自定义侦听器可能具有您想要的任意数量的参数
回答by Nargis
Callback can be very helpful in Java.
回调在 Java 中非常有用。
Using Callback you can notify another Class of an asynchronous action that has completed with success or error.
使用回调,您可以通知另一个类已成功或错误完成的异步操作。
回答by Orhun Mert Simsek
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.
在计算机编程中,回调是一段可执行代码,它作为参数传递给其他代码,期望在某个方便的时间回调(执行)该参数。调用可能是即时的,如在同步回调中,也可能在稍后发生,如在异步回调中。
回答by Orhun Mert Simsek
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.
我在以下情况下使用:在 UI 中,我从按钮获得了一个操作,例如。用户想要下载一个 500mb 的文件。谢谢我会初始化后台引擎(AsyncTask 类)并传参数给他。在 UI 上,我将显示一个阻止进度对话框并禁止用户进行任何其他点击。问题是:何时从 UI 中移除阻塞?答案是:当引擎以成功或失败结束时,可以是回调或通知。
What is the difference between notification and callbacks it is another question, but 1:1 is faster the callback.
通知和回调有什么区别是另一个问题,但 1:1 的回调速度更快。