This is an Rxbus2 library based on EventBus for Android. It simplifies the communication between Activities, Fragments etc.
Rxbus2 is pettern based on Publish & Subscribe Pattern
Advantages
- Loosely coupled, Easy to develop, Simply
Disadvantages
- Debugging is difficult, Not easy management.
- Use CompositeDisposable for efficient memory management.
- You can receive events from the BaseClass. (BaseActivity, BaseFragment etc...)
- You can register the same class using hascode.
Gradle dependency:
Add it in your root build.gradle at the end of repositories:
repositories {
// ...
maven { url 'https://jitpack.io' }
}
Add the gradle dependency to your app module build.gradle file:
dependencies {
implementation 'com.github.warrenth:RxBus2:1.0.0'
}
Get the latest dependency at jitpack.io.
- Register and unregister your subscriber.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxBus.get().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
RxBus.get().unResister(this);
}
- Post events. (Param Type : All of Obejct)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send_left:
RxBus.get().send(EventType.TAG.LEFT_MENU, new ArrayList<>());
break;
case R.id.btn_send_all:
RxBus.get().send(EventType.TAG.ALL, "Send ALL");
break;
case R.id.btn_send_right:
RxBus.get().send(EventType.TAG.RIGHT_MENU, 2018);
break;
}
}
- Define a function (Subscribe) in the class that will receive the event.
@Subscribe(eventTag = EventType.TAG.MIDDLE)
public void receiveMiddle(String receivedMsg) {
mTextContent.append(receivedMsg + "\n");
}
@Subscribe(eventTag = EventType.TAG.ALL)
public void receiveAll(String allMsg) {
mTextContent.append(allMsg + "\n");
}
- You can also receive events in the base class. If you need to receive events from the base class, Use as follows.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxBus.get().register(this, RxBusHelper.getRegistClass(this, BaseClass.class));
}
@Override
protected void onDestroy() {
super.onDestroy();
RxBus.get().unResister(this, RxBusHelper.getRegistClass(this, BaseClass.class));
}
Copyright 2018 warrenth, Inc.
The contents of this repository are licensed under the Apache-2.0