Android - ----- The Use of LiveDataBus

LiveData is a new technology presented at the 17-year Google IO conference. Compared with the framework EventBus and RxBus of communication bus type, it is simpler, simpler and more decoupled.


LiveEventBus is an Android message bus based on LiveData, with life cycle awareness, Sticky support, AndroidX support, cross-process support, cross-APP support.


Advantages of LiveDataBus

Implementation and Simplicity of LiveDataBus

Compared with the complex implementation of EventBus, LiveDataBus can be implemented in only one class.


LiveDataBus can reduce the size of APK packages

LiveDataBus relies only on the official Android component LiveData, which implements only one class. EventBus 57Kb, RxJava 2.2M


LiveDataBus Dependent Support is Better

LiveDataBus relies only on the official Android component LiveData, which is better supported by relying parties than the RxBus-dependent RxJava and RxAndroid.


LiveDataBus has life cycle awareness


LiveDataBus has life cycle awareness, and callers do not need to invoke anti-registration in Android system. Compared with EventBus and RxBus, LiveDataBus is more convenient and has no risk of memory leak.


Characteristics of LiveEventBus

Life cycle awareness, message subscription at any time, automatic cancellation of subscription
Support Sticky sticky messages
Support for AndroidX
Supporting cross-process communication
Supporting cross-APP communication
Support setting up the mode of receiving messages by Lifecycle Observer (such as Activity):

1. Messages can be received in real time throughout the life cycle (from onCreate to onDestroy)
2. Started can receive messages in real time. Stoped can not receive messages in real time. It is necessary to wait until Activity becomes active again before receiving messages.


Reference in Engineering
implementation 'com.jeremyliao:live-event-bus:1.4.2'

To configure
Configure in the Application.onCreate method:

LiveEventBus.get()
        .config()
        .supportBroadcast(this)
        .lifecycleObserverAlwaysActive(true);

With life cycle awareness, Lifecycle Owner automatically cancels subscriptions when it is destroyed without calling removeObserver

Ensure that the key values in with() are the same, (registration/reception) and initiate communication

Registration:

LiveEventBus.get().with("LiveDataBusDemo1",String.class).observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
Log.i("aaa",s);
}
});

Initiate communication

LiveEventBus.get().with("LiveDataBusDemo1").post("LiveDataBus1");

Subscribe and unsubscribe messages in Forever mode. Subscribe messages in Forever mode. You need to call removeObserver manually to unsubscribe.

register

private Observer<String> observer = new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
Log.i("aaa",s);
textView.setText("observeForever Registered observers receive messages: " + s);
}
};

LiveEventBus.get()
.with("LiveDataBusDemo2", String.class)
.observeForever(observer);

Initiate communication

LiveEventBus.get().with("LiveDataBusDemo2").post("LiveDataBus2");

Manual unsubscribe message

LiveEventBus.get()
.with("LiveDataBusDemo2", String.class)
.removeObserver(observer);

Sticky mode (information that initiates communication before or before registration can be received)

Support Sticky mode when subscribing to messages, so that subscribers can receive messages sent before, but also support messages sent later.

Subscribe to messages in Sticky mode with life cycle awareness. Lifecycle Owner automatically cancels subscriptions when it is destroyed without calling removeObserver

 

register

LiveEventBus.get()
.with("LiveDataBusDemo3", String.class)
.observeSticky(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
Log.i("aaa",s);
textView.setText("observeSticky Registered observers receive messages: " + s);
}
});

Initiation communication (you can distinguish the initiation notification before and after registration to see the effect)

LiveEventBus.get().with("LiveDataBusDemo3").post("LiveDataBus3");

Observe Sticky Forever, Forever mode subscription message, need to manually call removeObserver to cancel subscription, as above


register

private Observer<String> observer = new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
Log.i("aaa",s);
textView.setText("observeStickyForever Registered observers receive messages: " + s);
}
};


LiveEventBus.get()
.with("LiveDataBusDemo4", String.class)
.observeStickyForever(observer);

Initiate communication

LiveEventBus.get().with("LiveDataBusDemo4").post("LiveDataBus4");

Manual unsubscribe message


LiveEventBus.get()
.with("LiveDataBusDemo4", String.class)
.removeObserver(observer);

Confusion rule

-dontwarn com.jeremyliao.liveeventbus.**
-keep class com.jeremyliao.liveeventbus.** { *; }
-keep class android.arch.lifecycle.** { *; }
-keep class android.arch.core.** { *; }

Reference documents:
https://github.com/JeremyLiao/LiveEventBus

Keywords: Mobile Android Google github

Added by lional on Sat, 14 Sep 2019 17:51:58 +0300