本文介绍了虚拟回调接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在从谷歌的例子主从流code了Eclipse为您创建,在片段那里有这种东西:
In the example master-detail-flow code from Google that Eclipse creates for you, theres this stuff in the fragment:
private Callbacks mCallbacks = sDummyCallbacks;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
我明白一个回调接口如何使用它包含活动从一个片段沟通,但什么是这个虚拟回调好?
I understand how a callback interface is used to communicate from a fragment to it's containing Activity, but what is this dummy callback good for?
推荐答案
伪回调,以避免使用时,它测试回调的有效性的需要。
The dummy callback is made to avoid the need for testing the validity of the callback when using it.
另一种方式来复位中的回调 onDetach
被设置为null,但这需要空检测每一个使用它的时候,这是一个很大重复/枯燥code。
The other way to 'reset' the callbacks in onDetach
is to set it to null, but that would require null testing every time it is used, which is a lot of repetitive/boring code.
这篇关于虚拟回调接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!