[url] github.com/ewgcat/Live…
The introduction of LiveData
LiveData is a framework proposed by Android Architecture Components. LiveData is an observable data holding class that senses and follows the life cycle of components such as activities, fragments, or Services. Because of the component lifecycle awareness nature of LiveData, it is possible to update UI data only when the component is in the active state of the lifecycle.
LiveData requires an Observer object, typically a concrete implementation of the Observer class. LiveData notifies the observer of data changes when the observer’s life cycle is STARTED or RESUMED. LiveData will not be notified even if its data changes while the observer is in another state.
The advantages of LiveData
The UI is consistent with the LiveData because LiveData is in observer mode, so you can be notified when the data changes and update the UI.
To avoid memory leaks, the observer is tied to the life cycle of the component and automatically cleans up its own data as soon as the bound component is destroyed.
There will no longer be a crash caused by the Activity being stopped, for example, while the Activity is in the background, it will not receive any events from LiveData.
Instead of having to deal with life cycle issues, LiveData can sense the life cycle of bound components and only notify data changes when active.
Real-time data refresh, always receiving the latest data when a component is active or inactive to active.
Solve the Configuration Change problem, when the screen rotates or is recycled and restarted, you can receive the latest data immediately.
Important: Why use LiveData to build the data communication bus LiveDataBus
Reasons for using LiveData
LiveData’s observable and life-cycle awareness capabilities make it an ideal building block for Android’s communication bus.
The consumer does not have to invoke the de-register method explicitly.
Because of LiveData’s lifecycle awareness, LiveDataBus only needs to invoke the registration callback method and does not need to invoke the de-registration method shown. This has the advantage of not only writing less code, but also completely eliminating the risk of memory leaks when other communication bus class frameworks (such as EventBus and RxBus) forget to call de-registration.
2. Why use LiveDataBus instead of EventBus and RxBus
(1) The implementation of LiveDataBus is extremely simple. Compared with the complex implementation of EventBus, LiveDataBus only needs one class to implement. LiveDataBus can reduce the size of the APK package because LiveDataBus only relies on LiveData of Android’s official Android Architecture Components and has no other dependencies and only a single class implementation. For comparison, the EventBus JAR package size is 57kb, and RxBus relies on RxJava and RxAndroid, where the RxJava2 package size is 2.2MB, the RxJava1 package size is 1.1MB, and the RxAndroid package size is 9kb. Using LiveDataBus can greatly reduce the size of APK packages.
(2) Better support for LiveDataBus dependencies, LiveDataBus only relies on the official Android Architecture Components of LiveData, compared to RxJava and RxAndroid which RxBus relies on. Dependent party support is better.
(3) LiveDataBus has life cycle awareness, LiveDataBus has life cycle awareness, using the caller in Android system does not need to call anti-registration, compared to EventBus and RxBus more convenient, and there is no risk of memory leakage.
[url] github.com/ewgcat/Live…