在EventBus的fragments之间没有注册事件类的订阅者

我正在尝试在两个fragments之间使用GreenRobot EventBus,但是我仍然没有注册任何事件类的订阅者。在我的情况下,我在底部导航栏中有两个fragments,没有任何按钮。因此,我点击栏上的图标并更改fragment,然后从第一个fragment EventBus获取字符串并传递给第二个fragment。

第一个fragment(Sender):

private void sendLocation(String location) {
    EventBus.getDefault().post(new BusEvent(location));
}

@Override
public void onStop() {
    super.onStop();
    sendLocation(location);
    Log.d("TF ", location);
}

第二个fragment(recipient):

@Subscribe(threadMode = ThreadMode.MAIN)
public void onBusEvent(BusEvent event) {
    String location = event.location;
    hourlyViewModel.location.set(location);
}

@Override
public void onAttachFragment(@NonNull Fragment childFragment) {
    super.onAttachFragment(childFragment);
    EventBus.getDefault().register(this);
}

@Override
public void onDetach() {
    super.onDetach();
    EventBus.getDefault().unregister(this);
}

事件类:

public class BusEvent {

public String location;

public BusEvent(String location) {
    this.location = location;
}

public String getLocation() {
    return location;
}
}

日志:

    D/EGL_emulation: eglMakeCurrent: 0xae414900: ver 3 0 (tinfo 0xaa403f20)
W/art: 在Android 4.1之前,int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean)方法会错误地覆盖android.widget.ListView中的私有方法
I/Choreographer: 跳过39帧!应用程序可能在其主线程上做太多的工作。
D/OkHttp: --> GET http://api.openweathermap.org/data/2.5/weather?q=Rome&appid=52e6ff60bba8613b4850e065dcd3d0ac&units=metric
    --> END GET
D/OkHttp: <-- 200 OK http://api.openweathermap.org/data/2.5/weather?q=Rome&appid=52e6ff60bba8613b4850e065dcd3d0ac&units=metric (432ms)
D/OkHttp: 服务器:openresty
    日期:201912716:10:39 GMT
    内容类型:application/json; charset=utf-8
    内容长度:432
    连接:保持活动状态
    X-Cache-Key:/data/2.5/weather?q=rome&units=metric
    访问控制允许来源:*
    访问控制允许凭证:true
    访问控制允许方法:GET、POST
D/OkHttp: {"coord":{"lon":12.48,"lat":41.89},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"stations","main":{"temp":10.28,"pressure":1003,"humidity":76,"temp_min":8,"temp_max":12},"visibility":10000,"wind":{"speed":6.2,"deg":150},"clouds":{"all":75},"dt":1548604200,"sys":{"type":1,"id":6792,"message":0.0036,"country":"IT","sunrise":1548570428,"sunset":1548605946},"id":6539761,"name":"Rome","cod":200}
    <-- END HTTP (432-byte body)
D/CWR: androidx.lifecycle.MutableLiveData@228dfe8
W/Glide: 未能找到GeneratedAppGlideModule。你应该在你的应用程序中包括一个annotationProcessor编译依赖于com.github.bumptech.glide:compiler并且一个@GlideModule注释的AppGlideModule实现,否则LibraryGlideModules会被默默地忽略
D/EventBus: 没有为com.example.daniellachacz.weatherapp2.view.BusEvent事件类注册订阅者
D/EventBus: 没有为org.greenrobot.eventbus.NoSubscriberEvent事件类注册订阅者
D/TF: Rome
点赞
用户4758255
用户4758255

这是因为你没有在片段中调用 register EventBus。你尝试使用 onAttachFragment 注册 EventBus,但它在片段将另一个片段作为其子片段附加时才会被调用。正如文档所说:

public void onAttachFragment (Fragment childFragment) Called when a fragment is attached as a child of this fragment.

This is called after the attached fragment's onAttach and before the attached fragment's onCreate if the fragment has not yet had a previous call to onCreate.

因此,你需要使用 onAttached

你的代码应该像这样:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    EventBus.getDefault().register(this);
}

@Override
public void onDetach() {
    super.onDetach();
    EventBus.getDefault().unregister(this);
}

或者像 EventBus 文档 中使用 onStart()onStop()

2019-01-27 15:19:06