Я пытаюсь использовать GreenRobot EventBus между двумя фрагментами, но у меня все еще нет подписчиков, зарегистрированных для класса событий. В моем случае у меня есть два фрагмента в нижней панели навигации и нет никаких кнопок. Итак, я нажимаю значок на панели и меняю фрагмент, затем из первого фрагмента EventBus берет строку и переходит ко второму.
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 (получатель):
@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: 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