我在项目中使用了EventBus。我有一个导航抽屉,我在其中浏览片段。我面对的是我正在尝试使用onCreateView()
GlobalBus.getBus().register(this)
中注册EventBus
然后我打开抽屉并打开第二个片段。然后,当我打开抽屉并重新打开第一个片段时,再次调用onCreateView()
,再次调用GlobalBus.getBus().register(this)
。再次注册EventBus(两次,三次)。
为了不再次注册,我做了
if (!GlobalBus.getBus().isRegistered(this))
GlobalBus.getBus().register(this)
但是它总是返回 false 。
我已从
取消注册EventBusoverride fun onDestroyView() {
GlobalBus.getBus().unregister(this)
}
谁能告诉我为什么
if (!GlobalBus.getBus().isRegistered(this))
GlobalBus.getBus().register(this)
不起作用?而且我如何才能为任何片段仅注册一次,以使订户方法不会多次调用?
答案 0 :(得分:0)
我正在这样使用。
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerBus();
}
@Override
public void onDestroy() {
super.onDestroy();
unRegisterBus();
}