片段在恢复后不响应UI更新和事件总线事件

VSB*_*VSB 5 android android-fragments notifydatasetchanged fragment-lifecycle greenrobot-eventbus

我有一个SearchFragment类扩展了其中的类BaseFragmentonResume并被onStop覆盖如下:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

getFragmentManager().beginTransaction().replace(R.id.container, new ProductDetailFragment()).addToBackStack(null).commit();
Run Code Online (Sandbox Code Playgroud)

此外,我的片段中的其他一些事件也不起作用。我的片段有一个listView,它没有响应notifyDataSetChanged()

从中返回后ProductDetailFragment,不会触发eventbus订阅者,并且某些事件(例如notifyDataSetChanged属于我的listview的适配器)不会响应并且不会在UI上反映更改。

从中返回ProductDetailFragment时,当控制权到达SearchFragment.onResumeeventbus 时,调试行的代码仍在注册,并且不需要再次注册,但是生成的事件不会触发订阅者。

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

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

onAttach
onCreate
onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu
Run Code Online (Sandbox Code Playgroud)

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

onPause
onStop
onDestroyView
onDestroyOptionsMenu
Run Code Online (Sandbox Code Playgroud)

返回此片段的生命周期事件:

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu
Run Code Online (Sandbox Code Playgroud)

ישו*_*ותך 3

您可以看到,onStop()当片段被替换时,您被调用,因此 EventBus 未注册:

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

onPause
onStop
onDestroyView
onDestroyOptionsMenu
Run Code Online (Sandbox Code Playgroud)

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

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

onCreateView
onViewCreated
onViewCreated
onStart
onResume
onCreateOptionsMenu
onPrepareOptionsMenu
Run Code Online (Sandbox Code Playgroud)

但是当您从 ProductDetailFragment 返回时,您的片段onResume()尚未被调用。因此不会调用片段中的 subscribe 方法。