假设我有这个片段:
public class FragmentA extends Fragment {
public interface CallerProxy {
public void publishParams(Object... dataParams);
}
private CallerProxy mCallerProxy;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof CallerProxy) {
this.mCallerProxy = (CallerProxy) activity;
}
}
@Override
public void onDetach() {
super.onDetach();
this.mCallerProxy = null;
}
protected void sendCustomMessage(Object... objects) {
if (mCallerProxy != null) {
mCallerProxy.publishParams(objects);
}
}
}
将接口定义为静态或非静态有什么区别?即使我声明CallerProxy
为静态的,那么在父活动中我也会有这个:
public class MyProxyActivity extends FragmentActivity implements CallerProxy {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(...) and other relevant code
}
@Override
public void publishParams(Object... dataParams) {
// /
}
}
据我所知,如果接口被声明为静态,那么接口将绑定到FragmentA
类对象,如果我不将其声明为静态,那么它将绑定到FragmentA
实例对象 - 这会导致内存泄漏吗?
如果界面不是静态的,可能会出现什么问题,为什么?