Stack Overflow archive
0 scoreaccepted

How to use network communication using greenrobot eventbus?

score
0
question views
425
license
CC BY-SA 3.0

I would suggest using an architecture where an event bus might not be required. An event bus is still useful and I think you can find what you are looking for in their getting started guide.

Some example code:

java
public class EventBusExample extends Activity {

  @Override protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
    EventBus.getDefault().post(new BackgroundWorkEvent());
  }

  @Override protected void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
  }

  @Subscribe(threadMode = ThreadMode.ASYNC)
  public void doBackgroundWork(BackgroundWorkEvent event) {
    // do background work here
    // when finished, post to ui thread
    EventBus.getDefault().post(new UiWorkEvent());
  }

  @Subscribe(threadMode = ThreadMode.MAIN)
  public void doUiWork(UiWorkEvent event) {
    // on main thread. do ui stuff
  }

  public static class BackgroundWorkEvent {

  }

  public static class UiWorkEvent {

  }

}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.