I'm trying to use the GreenRobot EventBus between two fragments, but I have still No subscribers registered for event class. In my case I have two fragments in bottom navigation bar and there is no any buttons. So I'm clicking the icon on bar and changing fragment, then from the first fragment EventBus takes string and passing to the second one.
FirstFragment(Sender):
private void sendLocation(String location) {
EventBus.getDefault().post(new BusEvent(location));
}
@Override
public void onStop() {
super.onStop();
sendLocation(location);
Log.d("TF ", location);
}
SecondFragment(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);
}
Event:
public class BusEvent {
public String location;
public BusEvent(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
}
Logs:
D/EGL_emulation: eglMakeCurrent: 0xae414900: ver 3 0 (tinfo 0xaa403f20)
W/art: Before Android 4.1, method int androidx.appcompat.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
I/Choreographer: Skipped 39 frames! The application may be doing too much work on its main thread.
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: Server: openresty
Date: Sun, 27 Jan 2019 16:10:39 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 432
Connection: keep-alive
X-Cache-Key: /data/2.5/weather?q=rome&units=metric
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: 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: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
D/EventBus: No subscribers registered for event class com.example.daniellachacz.weatherapp2.view.BusEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent
D/TF: Rome
This is because you haven't call the register EventBus in your Fragment. You're trying to register the EventBus with onAttachFragment, but it won't be called until the fragment attached another fragment as its child. As the documentation says:
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.
So, you need to use onAttached instead.
Your code should be something like this:
@Override
public void onAttach(Context context) {
super.onAttach(context);
EventBus.getDefault().register(this);
}
@Override
public void onDetach() {
super.onDetach();
EventBus.getDefault().unregister(this);
}
or use onStart()
and onStop()
like in the EventBus documentation.