Posts

Showing posts from September, 2017

Android Architecture Components : ViewModel

Image
ViewModel : UI-Related Data ObjectOne 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.  BenefitsKeeps data across UI recreationNo need to repeat API calls and Database queries on UI recreation Keeps Activity/Fragments clean since work is now delegated to ViewModelsShare 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 and what exactly LiveData is used for in my post here.
Acce…

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() is called. This way the obs…

Android Architecture Components : Room Persistence Library

Image
Room: a SQLite object mapping library After many years and using countless 3rd party DAO libraries I am happy to finally introduce official Android solution to working with SQLite databases and it is called Room Persistence Library. Before we begin you can find code from this tutorial here.

SQL ReviewData is stored in tablesTable's rows contain entitiesTable's columns contain entity's fieldsRows can have id's (primary keys) and id's must be uniqueYou can create relationships between rows in different tables by referring to other row's id (foreign key)Index primary key columns for performanceUse SQL queries to find sort and filter the data from tables What is Room? Room is a library that uses annotations in your Java data models and your data access objects (DAO's) to generate database schema (table definitions) and SQLite queries. So you don't have to write all the SQLite queries from scratch and have logic for storing and retrieving your models from d…