Posts

Showing posts from September, 2017

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() ...

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 Review Data is stored in tables Table's rows contain entities Table's columns contain entity's fields Rows can have id's (primary keys) and id's must be unique You can create relationships between rows in different tables by referring to other row's id (foreign key) Index primary key columns for performance Use 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 r...