Monday 11 June 2018

Architecture Components In Android

What are Architecture Components? 


A Set Libraries from Google which will help Developer’s Design Applications :

1. Testable

2. Maintainable

3. Robust

Moreover, Handling UI part of the applications I.e Activity/Fragment Lifecycle and managing persistence stage. Introducing concept’s Like ViewModel to easily handle UI data when there are orientation changes.
There are various things to learn in Architecture Components as it makes Life easy for the Developer.Let’s Dig deep into these components and understand functionality of Each Component included here.


1. ViewModel

 Responsibility to Manage/Store UI (Activity/Fragment) related DATA, So that data is persist whenever there are configuration changes like Screen Orientation. ViewModel should not contains UI (Activity/Fragment) references.

Note : We do not need worry about Memory Leaks as ViewModel is LIFECYCLE aware.


2. LiveData 

As name is depicting, this will deal with Live data changes. This component hold’s value which can be observed by UI for changes or Can also be observed by ViewModel again depends upon your requirement.

Note : It help’s controlling crashes as it is LIFECYCLE aware I.e if UI (Activity/Fragment) is Alive to observe changes or Not.

There is LifeCycle Owner, In Android activity is Lifecycle owner. So whenever a activity changes states i.e pause, stop etc it is responsibility of the LiveData to decide whether to send data from observer or not.

Usage Of LiveData Types :


 1. LiveData : 

is very useful as using this we can listen to every change made to Room database I.e on every Sql query executed.

2. MutableLiveData : 

Is wrapping a data type inside LiveData. This must be used whenever you want to communicate between ViewModel and Activity. Good thing about this type is there no leak as it does not required any context.

3. MediatorLiveData : 

This is used to listen Other LiveData types and connects Repository to UI (Activity/Fragment) via ViewModel.

Room

Room Persistence Lib The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Learn more about Room from Below Link :

https://developer.android.com/training/data-storage/room/

Source Codehttps://github.com/CammyKamal/ArchComponentsKotlin

Cheers!!

No comments:

Post a Comment