Posts

Showing posts with the label android architecture

Android Architecture Components meets Data Binding : Lifecycle awareness!

Image
Finally Android Data Binding library got updated to play nice with Android Architecture Components library and became life-cycle aware! This means that any changes to the LiveData objects will be reflected in the layouts... automagically! :) * Tested on Android Studio 3.1 - canary 6 Whats new? To make the binding life-cycle aware you need to pass the life-cycle owner to the ViewDataBinding by calling the  setLifecycleOwner method. The ViewModel: Our view model consists of the LiveData object that is simply and observable data container, where the observer gets notified of data changes. The model: Our model has one field that is used to store user's name. A POJO. The layout: -Out layout consists of a text field that is bound to the user's name. And will be updated when our LiveData container posts a change. -We also have an EditText that is two-way bound to the user's name, meaning when text gets entered, the name is updated and vise-versa, when the use...

Android Architecture Components : ViewModel

Image
ViewModel : UI-Related Data Object One of the biggest challenges for an Android developer is keeping data across configuration changes such as screen rotation or during the recreation of Activity / Fragments after its been destroyed and recreated by the framework. Luckily the new Android Architecture Components library has ViewModel class that is intended to store and manage UI-related data so that the data survives configuration changes.  Benefits Keeps data across UI recreation No need to repeat API calls and Database queries on UI recreation  Keeps  Activity / Fragments clean since work is now delegated to ViewModels Share ViewModels between Fragments Subclass ViewModel To create a view-model just subclass ViewModel . View-models are responsible to provide data and keep reference to it. Our TimerViewModel lazily initializes TimerLiveData which is a LiveData implementation of a timer that counts seconds, read more about TimerLiveData a...

Android Architecture Components : LiveData

Image
Live Data : Life-cycle aware Observables As an Android developer you often have to work with data that is dynamic and changing with user input, device configuration changes, or even time changes. On top of this as a developer you have to respect the life-cycle of an Activity of Fragment that contains this dynamically changing data: stop updates onStop() or onPause() and restart the updates onStart() and onResume() . Examples of this is location data, countdown timers, user's list selections and any other type of dynamically changing data. Lets see how we can make these tasks easier using LiveData . We will demonstrate LiveData by creating a self contained timer. Subclass LiveData LiveData object is set active when the observer's lifecycle is stated or resumed . And the LiveData object is set inactive when the observer's lifecycle is stopped or pause . In our TimerLiveData we start timer when  onActive() is called and stop the timer when  onInactive() ...