我在custom interface
NetworkReceiver
public interface onNetworkStateChanged {
void onStateChange(int state);
}
我已将它们都设置在我的Activity和Fragment
上network = new NetworkReceiver();
network.setOnNetworkStateChanged(this);
---------------------------------------------
@Override
public void onStateChange(int state) {
if (state == NetworkReceiver.STATE_ON) {
networkState = state;
} else {
networkState = state;
}
}
当触发NetworkReceiver时,它始终是Activity内部的监听器,而不是片段中的监听器
有人有想法吗?或者我需要在我的片段中实现一个customCallback,该活动将触发调用Fragment?
编辑:
我把它放在活动的onStateChange上
Fragment f = getFragmentManager().findFragmentByTag(Utils.FEED_FRAGMENT);
if (f instanceof FeedFragment) {
((FeedFragment) f).onStateChange(state);
}
答案 0 :(得分:1)
很难说,因为你的代码不完整。您应该将片段注册为侦听器,似乎setOnNetworkStateChanged(this)
使用Activity实例。
或者在Activity中你可以找到Fragment并直接调用onStateChange(),这样你就可以获得链式调用(与onOptionsItemSelected相同的机制)。
答案 1 :(得分:-1)
//create a public method setNetworkChageListner() inside NetworkReceiver
public void setNetworkChageListner(onNetworkStateChanged onNetwokChanged){
this.onNetworkChanged=onNetworkChanged
}
/*check the global variable onNetworkChanged is null when you trigger the method onStateChange(int state)*/
//inside your fragment use receiver static variable of activity
netwok_receiver_static_variable.setNetworkChageListner(new NetworkReceiver.onNetworkStateChanged () {
@Override
public void onStateChange() {
//do operation
}
})