【问题标题】:Fragment does not respond to UI updates and eventbus events after resume恢复后片段不响应 UI 更新和事件总线事件
【发布时间】:2019-03-11 09:46:50
【问题描述】:

我有一个SearchFragment 类,它扩展了一个名为BaseFragment 的类,其中onResumeonStop 被覆盖如下:

@Override
public void onResume() {
  checkEventBusRegistration();
    super.onResume();
}
@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}
public void checkEventBusRegistration()
{
    if(!EventBus.getDefault().isRegistered(this))
    {
        EventBus.getDefault().register(this);
    }
}

SearchFragment 是显示搜索结果列表的片段。通过单击每个项目,产品的详细信息通过以下调用显示在其他片段上:

getFragmentManager().beginTransaction().replace(R.id.container, new ProductDetailFragment()).addToBackStack(null).commit();

此外,我的片段中的一些其他事件无法正常工作。我的片段有一个不响应 notifyDataSetChanged() 的 listView。

ProductDetailFragment返回后,eventbus订阅者没有被触发,一些属于我的listview适配器的事件如notifyDataSetChanged没有响应并反映在UI上的变化。

调试代码行,从ProductDetailFragment返回后,当控制到达SearchFragment.onResume时,仍然注册了eventbus,不需要再次注册,但生成的事件不会触发订阅者。

如果有帮助,这里是我的片段触发的生命周期事件:

创建片段的生命周期事件:

onAttach
onCreate
onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu

通过替换离开此片段时的生命周期事件:

onPause
onStop
onDestroyView
onDestroyOptionsMenu

返回这个片段时的生命周期事件:

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu

【问题讨论】:

  • 在 ondestroy 中注销并检查一次
  • @ManoharReddy 它没有帮助。在第一次运行片段时一切都很好,但是在移动到下一个片段并返回后它开始失败......
  • 你为什么在 onResume 方法中注册,在 onStop 方法中取消注册?使用配对的 onResume/onPause 不应该更容易吗?
  • @NicolaGallazzi 那么有什么区别呢?
  • @VSB 您不需要 checkEventBusRegistration(),onPause 会在 onResume 之后立即调用,您不会冒事件总线注册泄漏的风险。不知道这是否能解决你的问题,但我会试试看

标签: android android-fragments notifydatasetchanged greenrobot-eventbus fragment-lifecycle


【解决方案1】:

你可以看到你onStop()在片段被替换的时候被调用了,所以EventBus被注销了:

通过替换离开此片段时的生命周期事件:

onPause
onStop
onDestroyView
onDestroyOptionsMenu

然后,当您返回片段时,您的 onResume() 被调用,然后 EventBus 被注册:

返回这个片段时的生命周期事件:

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu

但是当您从 ProductDetailFragment 返回时,您的片段 onResume() 尚未被调用。因此片段中的订阅方法没有被调用。

【讨论】:

  • 我不明白这个:But when you returning back from ProductDetailFragment your fragment onResume() is not yet called?如您所见,onResume 在我返回片段时被调用
  • 当我返回到我的片段时,eventbus 不起作用,这是我的问题。
  • 我的意思是,您的 EventBus 仅在您返回片段时才注册。在您调用onResume() 之前,该事件不会被捕获。像这样:1. onStop() is called 然后事件总线未注册 2. 事件已发送但未捕获 3. onResume() is called 然后事件总线已注册
  • 替换片段时我的问题没有被触发。问题是当我返回(通过按返回按钮)并调用 onResume 并再次注册 eventBus 但它不响应事件时。
【解决方案2】:

如果您使用调试器从发布事件的位置逐步执行代码,进入EventBus.post()postSingleEvent() 再到postSingleEventForEventType(),那么subscriptions 的值是多少?如果变量为 null 或空且方法返回 false,则订阅有问题。如果不是,或者您在发布活动后从未访问过此代码,那么问题可能出在您代码中的其他地方。

我还建议在匹配的生命周期对中注册和取消注册事件订阅,在onStart() 中注册并在onStop()onResume()/onPause() 中取消注册。

如果您共享更多代码以查看其他可能存在问题的地方,将会很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多