Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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