Sharing resources You can extend a LiveData Proper configuration changes If an activity or fragment is recreated due to a configuration change, likeĭevice rotation, it immediately receives the latest available data. Latest data right after it returns to the foreground. For example, an activity that was in the background receives the Always up to date data If a lifecycle becomes inactive, it receives the latest data upon becomingĪctive again. Lifecycle status changes while observing. LiveData automatically manages all of this since it’s aware of the relevant No more manual lifecycle handling UI components just observe relevant data and don’t stop or resume observation. The back stack, then it doesn’t receive any LiveData events. No crashes due to stopped activities If the observer's lifecycle is inactive, such as in the case of an activity in No memory leaks Observers are bound toĬlean up after themselves when their associated lifecycle is destroyed. Time the app data changes because the observer does it for you. That way, you don't need to update the UI every You can consolidate your code to update the UI in Using LiveData provides the following advantages: Ensures your UI matches your data state LiveData follows the observer pattern. Unsubscribed when their lifecycles are destroyed.įor more information about how to use LiveData, see Work with LiveData This is especially useful for activities and fragments because they can safelyĪnd not worry about leaks-activities and fragments are instantly This relationship allows the observer to be removed when the state of You can register an observer paired with an object that implements the LiveData only notifies active observers about updates. LiveData considers an observer, which is represented by the Note: To import LiveData components into your Android project, see Adding This awareness ensures LiveData only updates appĬomponent observers that are in an active lifecycle state. Meaning it respects the lifecycle of other app components, such as activities,įragments, or services. Unlike a regular observable, LiveData is lifecycle-aware,
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |