Skip to content

The Android Event Bus base on RxJava like Event bus API

Notifications You must be signed in to change notification settings

Doublemine/RxBus

Repository files navigation

RxBus

Usage:

Step 1. Add the JitPack repository to your build file

	allprojects {
		repositories {
			maven { url "https://jitpack.io" }
		}
	}

Step 2. Add the dependency

	dependencies {

	     compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
         compile 'io.reactivex.rxjava2:rxjava:2.0.5'
	     compile 'com.github.Doublemine:RxBus:2.1.2-kotlin'

	}

Step 3. How To Use:

Recommend (Like event bus API)

  • Define events:
 public class MessageEvent { /* Additional fields if needed */ }
  • Prepare subscribers: Declare and annotate your subscribing method.

NOTE: The method of subscribed only support single argument.

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};

Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

@Override
public void onStart() {
    super.onStart();
    RxBus.Companion.get().register(this);
}

@Override
public void onStop() {
    super.onStop();
    RxBus.Companion.get().unRegister(this);
}
  • Post events:
RxBus.Companion.get().post(new MessageEvent());

UN-Recommend (The Old API)

  • post a event Msg
/**
     *
     * do something
     *
     * */

    RxBus.Companion.get().post(new EventMsg("send some message..."));
  • receive event Msg
  CompositeDisposable  compositeDisposable = new CompositeDisposable();

    /**
    *
    * some code...
    *
    */
  compositeDisposable.add(RxBus.Companion.get()
          .asFlowableFilterType(EventMsg.class)
          .subscribeOn(AndroidSchedulers.mainThread())
          .subscribe(new Consumer<Object>() {
            @Override public void accept(Object o) throws Exception {
              EventMsg eventMsg = (EventMsg) o;
              /**
                * do something
               **/
            }
          }));

TODO-List

  • Support Sticky Event