Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Created May 7, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunusemredilber/8315787baff7d7ad19794d9d706cc8ba to your computer and use it in GitHub Desktop.
Save yunusemredilber/8315787baff7d7ad19794d9d706cc8ba to your computer and use it in GitHub Desktop.
Asynchronous and event-based data passing in Android with RxJava
public final class RxBus {
// String can be replaced with any kind of Object.
private static final BehaviorSubject<String> behaviorSubject
= BehaviorSubject.create();
public static Disposable subscribe(@NonNull Consumer<String> action) {
return behaviorSubject.subscribe(action);
}
public static void publish(@NonNull String value) {
behaviorSubject.onNext(value);
}
}
/* Usage example
// In sender instance
RxBus.publish("Yunus says hi!");
// In reciever instance
Disposable disposable = RxJsBus.subscribe(value -> {
// Do anything with the value
});
// In reciver, don't forget to dispose connection when you are done. Example:
@Override
protected void onDestroy() {
super.onDestroy();
disposable.dispose();
}
*/
// RxJava: https://github.com/ReactiveX/RxJava
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment