When data changes you will want the updated data displayed instantly on the relevant active app screen. This means we have to observe the data so that when it changes, the app can react. Observing changes to data across multiple components of your app can be problematic; this is where the Android LiveData class comes into its own.
LiveData holds data that can be observed within a given app lifecycle so that an "Observer" (a callback that can receive data) can be paired with a "LifecycleOwner" (an Android lifecycle class used by custom components to handle lifecycle changes), and this observer will then be notified about any modifications to the data so the active screen is instantly updated. This only occurs on live active screens so we don't risk memory leaks because the bespoke Android app we have developed for you is continually updating screens which are currently hidden (e.g. you have taken a phone call or are using another app).
The advantages of using LiveData
Using LiveData provides the following advantages:
Screen data always up-to-date
Because LiveData notifies Observer objects when the lifecycle state changes (e.g. a screen becomes active) we can code our bespoke Android apps to update the UI using Observer objects so we update the UI every time there's a change.
No memory leaks
Memory leaks are a big no-no on smartphones and tablets because these devices don't have a lot of spare memory (how many times have you seen a poorly coded app crash - lots of the time due to an out-of-memory exception). Because Observers are bound to Lifecycle objects, and clean up after themselves when their associated lifecycle is destroyed, we can avoid the risk of memory leaks caused by data updating inactive UI screens.
No crashes due to stopped activities
If the observer's lifecycle is inactive (i.e. the app screen isn't currently active for that user) then it doesn’t receive any LiveData events.
No more manual lifecycle handling
UI components continually observe relevant data and won't stop or resume observation - LiveData automatically manages this because it is aware of all lifecycle status changes while it is observing.
Always up to date data
If a lifecycle becomes inactive (e.g. in background), it receives the latest data as soon as it is active again (e.g. returns to the foreground).
Correct configuration changes
If a screen is recreated (e.g. the user rotates the device from portrait to landscape view), that screen will immediately receive the latest available data.
We can extend a LiveData object to integrate with system services so that they can be shared in our bespoke Android app. The LiveData object can connect to the system service just the once and then any observer that needs the system service resource can just watch the LiveData object instead.