Skip to content

Instantly share code, notes, and snippets.

@PierceZ
Last active July 20, 2017 12:18
Show Gist options
  • Save PierceZ/5cd43571900628e286e50b90a7f17a6a to your computer and use it in GitHub Desktop.
Save PierceZ/5cd43571900628e286e50b90a7f17a6a to your computer and use it in GitHub Desktop.
public final class RxBus {
private static PublishSubject<Object> sSubject = PublishSubject.create();
private RxBus() {
// hidden constructor
}
public static Disposable subscribe(@NonNull Consumer<Object> action) {
return sSubject.subscribe(action);
}
public static void publish(@NonNull Object message) {
sSubject.onNext(message);
}
}
//Example usage below:
public class MyActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Using Retrolambda
RxBus.subscribe((message) -> {
if (message instanceof Data) {
Data data = (Data) cityObject;
Log.v("Testing", data.getInfo());
}
});
}
}
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
//Using Retrolambda
v.findViewById(R.id.view).setOnClickListener(view -> {
RxBus.publish(new Data("Hello World"));
});
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment