我在同一个活动中有三个片段,让我们说 FragmentA , FragmentB 和 FragmentC
假设FragmentA向FragmentB发送一个int,所以在FragmentA中我有:
//FRAGMENT_A
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getBus().register(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EventBus.getBus().register(this);
}
private void registerEventBus(int i) {
EventBus.getBus().post(i);
}
FragmentB收到了int:
//FRAGMENT_B
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getBus().register(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
EventBus.getBus().unregister(this);
}
@Subscribe
public void getBusData(int data) {
//whatever
}
但是现在我必须从FagmentB发送一个双重到FragmentC,我已经创建了一个新的Bus类,所以现在在FragmentB中,我有
//Fragment_B
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getBus().register(this);
EventBus2.getBus.register(this)
}
@Override
public void onDestroyView() {
super.onDestroyView();
EventBus.getBus().unregister(this);
EventBus2.getBus..unregister(this)
}
@Subscribe
public void getBusData(int data) {
//whatever
}
private void createEventBus2(double number){
EventBus2.post(number);
}
最后是FragmentC
//FragmentC
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus2.getBus().register(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
EventBus2.getBus().unregister(this);
}
@Subscribe
public void getBus2Data(double data) {
//whatever
}
从FragmentA到FragmentB工作得很好,但在FragmentC中我从来没有得到双数据值。
我做错了什么?