Before Google I/O 2017, Google abhorred advising any accurate architectonics for Android development. You could use Archetypal View Presenter (MVP), Archetypal View Controller (MVC), Model-View-ViewModel (MVVM), some added pattern, or alike no arrangement at all.
With the absolution of the Android Architectonics Components project, we assuredly accept an official advocacy for Android Appliance Architecture, and all the apparatus we charge to apparatus it.
The Android Architectonics Apparatus activity spans a cardinal of libraries, but back Room and Lifecycles afresh saw their aboriginal abiding release, I’ll focus on how to set up and use both of these production-ready libraries.
By the end of this tutorial, you should apperceive how to abstain anamnesis leaks and appliance crashes by creating apparatus that are able of managing their own lifecycles, and how to abundance abstracts locally in SQLite using abundant beneath boilerplate code.
>> An SQLite album for Android app developers
Most Android apparatus accept a lifecycle associated with them. You acclimated to be amenable for managing your application’s lifecycle, which wasn’t consistently easy, abnormally if your appliance featured assorted asynchronous calls accident simultaneously.
Failing to administer appliance lifecycles accurately can aftereffect in all kinds of issues, alignment from anamnesis leaks, to your appliance accident the user’s progress, and alike absolute appliance crashes.
Even if you managed the lifecycle correctly, implementing all of that lifecycle-dependent cipher in your lifecycle methods (such as onStart and onStop) could accomplish them aggrandized and complex. This makes methods difficult to read, maintain, and test.
The Lifecycles library offers a new admission to lifecycle management, by accouterment all the classes and interfaces bare to actualize lifecycle-aware apparatus that acclimatize their behavior automatically, and alike accomplish altered accomplishments in acknowledgment to lifecycle events.
As of Abutment Library 26.1.0, Fragments and Activities accept a Lifecycle article absorbed to them, which lets them advertisement their lifecycle accompaniment to added apparatus aural your application. For example, if an Activity appearance video content, afresh you can use this new lifecycle acquaintance to ascertain and abeyance the video automatically whenever the Activity loses focus, and restart it as anon as your appliance regains the foreground.
Since this action is automated, Lifecycles helps you abstain abounding problems acquired by lifecycle mismanagement. Back you’re not abstraction all of that lifecycle-dependent cipher into your lifecycle methods, your cipher is activity to be abundant added organized, so it’ll be easier to read, maintain, and test.
To use the Lifecycles library, you charge to add the afterward to your module-level build.gradle file:
If you appetite to use Java 8.0 with the Lifecycles library, afresh you’ll additionally charge to add the following:
The Lifecycles library introduces the afterward components:
The @OnLifecycleEvent comment can acknowledge to the afterward lifecycle events:
ON_ANY is triggered by any lifecycle event. If you use Lifecycle.Event.ON_ANY, afresh the adjustment should apprehend a LifecycleOwner and Lifecycle.Event argument.
Let’s attending at how you’d actualize a LifecycleObserver that responds to changes in an Activity’s state. In the afterward code, we’re press a bulletin to Android Studio’s Logcat whenever the associated LifecycleOwner (MainActivity) enters a started or chock-full state:
You can additionally handle assorted lifecycle contest aural the aforementioned method:
If your activity already has methods that handle lifecycle events, you can add the @OnLifecycleEvent comment to these absolute methods, rather than re-writing your accepted implementation.
See Also: hack viberYou’ll usually alone appetite to accomplish an operation back a Lifecycle is in a assertive state.
For example, if you attack to accomplish a FragmentTransaction afterwards an Activity accompaniment has been saved, afresh the FragmentManager is activity to bandy an exception. By appliance getState.isAtLeast, you can ensure this operation alone happens back the Lifecycle is in a accordant state:
The isAtLeast adjustment can analysis for the afterward Lifecycle states:
You can additionally retrieve the accepted lifecycle accompaniment by calling getCurrentState().
The Lifecycles library additionally provides the foundation for added Android Architectonics Components, including LiveData, which you can use alongside the Room abstracts chain library.
LiveData is an appreciable adhesive that can authority any data, including Lists. Once a LiveData has a value, it’ll acquaint its assigned Observers whenever that bulk changes.
However, clashing a approved Observable, LiveData is lifecycle-aware, so it alone updates Observers that are in an “active” accompaniment (i.e STARTED or RESUMED). If the LifecycleOwner alcove a Lifecycle.State.DESTROYED state, afresh the LiveData will abolish the Observer automatically. This lifecycle-awareness helps you abstain the crashes and errors that can action if you try to amend a chock-full Activity or Fragment.
Whenever a basic enters the STARTED state, it automatically receives the best contempo bulk from the LiveData article it’s observing. If an Activity or Fragment is resumed, or recreated as allotment of a agreement change, afresh it’ll accept the latest data.
To use the LiveData basic in your project, you’ll charge to add the afterward to your module-level build.gradle file:
If your app handles a cogent bulk of structured data, you can generally advance the user acquaintance by caching some of this abstracts locally. If you abide all of your app’s best important abstracts afresh users will be able to abide appliance your application, alike back they don’t accept an internet connection.
While Android has accurate the SQLite abstracts chain band-aid back adaptation 1.0, the congenital APIs are adequately low-level and implementing them requires a cogent bulk of time, effort, and boilerplate code. There’s additionally no compile-time analysis of raw SQL queries, and if the abstracts changes afresh you’ll charge to manually amend your SQL queries to reflect these changes, which can be time-consuming and error-prone.
Room is an SQLite mapping library which aims to lower the barrier to appliance SQLite on Android. Room abstracts some of the basal accomplishing capacity of creating and managing an SQLite database and lets you concern abstracts after accepting to use Cursors or Loaders. Room additionally provides compile-time validation of queries and entities, and armament you to accomplish database operations on a accomplishments thread, so you don’t aback wind up blocking Android’s all-important capital UI thread.
To use Room, add the afterward to your project’s module-level build.gradle file:
In Room, operations such as Insert, Amend and Annul are annotated, which is why we charge to add the comment processor as a activity dependency.
There are three above Room components:
The @Database chic provides the accomplished amid your appliance and SQLite.
Your @Database chic charge be an abstruse chic that extends RoomDatabase, which defines tables present in your database, provides Abstracts Admission Altar (DAO) classes, and includes a account of entities associated with the database.
You can admission an instance of Database by calling Room.databaseBuilder() or Room.inMemoryDatabaseBuilder().
Room creates a table for anniversary chic that you comment with @Entity, area anniversary acreage corresponds to a cavalcade in the table. Article classes are usually baby archetypal classes that don’t accommodate any logic.
Room can alone abide fields it has admission to, so you either charge to accomplish a acreage public, or accommodate getter and setter methods. Anniversary article additionally needs to ascertain at atomic one acreage as a primary key. Alike if there’s alone a distinct field, you’ll still charge to comment that acreage with @PrimaryKey.
Room uses the chic name as the database table name, unless you override it appliance the tableName property:
Room additionally derives the cavalcade name from the acreage name, unless you absolutely ascertain the cavalcade name appliance the @ColumnInfo(name = “column_name”) annotation, for example:
Room creates a cavalcade for anniversary acreage that’s authentic in the entity. If there’s a acreage you don’t appetite to persist, afresh you’ll charge to comment it with @Ignore.
Even admitting best object-relational mapping libraries let you map relationships from a database to the agnate article model, Room doesn’t acquiesce article references. The acumen abaft this brake is that this blazon of apathetic loading about occurs on Android’s capital UI thread, which can aftereffect in above user interfaces and appliance crashes. Instead, you’ll charge to absolutely appeal the abstracts your app requires.
You admission your abstracts via Abstracts Admission Altar (DAO), which can either be an interface or an abstruse class, but charge accommodate all the methods you appetite to use in your database queries.
These annotated methods accomplish the agnate SQL at abridge time, abbreviation the bulk of boilerplate you charge to address and maintain.
There’s several accessibility queries you can use with DAOs, including:
The afterward DAO interface contains assorted operations that we can accomplish on our table:
Each @Query adjustment is arrested adjoin the table schemas at abridge time. If there’s a botheration with a query, you’ll get a accumulation absurdity rather than a runtime failure.
When assuming queries, you’ll generally appetite your appliance to amend automatically back the abstracts changes. You can accomplish this by appliance Room in aggregate with LiveData – specifically, by appliance a acknowledgment bulk of blazon LiveData in your concern method. Room will afresh accomplish all the cipher all-important to amend the LiveData back the database is updated.
If you’re appliance the RxJava library in your project, you can actualize Room queries that acknowledgment a backpressure-aware Flowable. Flowable is a new accession to RxJava 2.0 that helps you abstain the affair of a antecedent Appreciable abounding items too bound for the after Observer to process. This can aftereffect in a excess of unconsumed, memory-hogging items.
To use RxJava with Room, you’ll charge to add the afterward annex to your module-level build.gradle file:
If you’re absorbed in acquirements added about appliance RxJava 2.0 in aggregate with the Room library, afresh Google has appear a Room and RxJava Sample app.
Even admitting Room is Google’s recommended admission for alive with databases, Android still supports absolute database admission appliance SQLite, so you don’t charge to about-face to Room.
Creating lifecycle-aware components
LiveData: Keep clue of alteration data
Easier abstracts accumulator with Room
dependencies { accomplishing "android.arch.lifecycle:runtime:1.0.3" annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
implementation "android.arch.lifecycle:common-java8:1.0.0"
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.arch.lifecycle.Lifecycle; import android.arch.lifecycle.LifecycleObserver; import android.util.Log; import android.arch.lifecycle.OnLifecycleEvent; public chic MainActivity extends AppCompatActivity { clandestine changeless final String TAG = "MainActivity"; @Override adequate abandoned onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Associate the LifecycleOwner with the LifecycleObserver, appliance getLifecycle().addObserver// getLifecycle().addObserver(new Observer()); } //Create an Observer that accouterments the LifecycleObserver interface// accessible chic Observer accouterments LifecycleObserver { //Use @OnLifecycleEvent to accept for altered lifecycle events// @OnLifecycleEvent(Lifecycle.Event.ON_START) accessible abandoned onStart() { Log.e(TAG, "ON_START"); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) accessible abandoned onStop() { Log.e(TAG, "ON_STOP"); } } }
@OnLifecycleEvent({ON_STOP, ON_START})
public abandoned startFragmentTransaction() { if (lifecycle.getState.isAtLeast(STARTED)) { //Perform the transaction// } }
implementation "android.arch.lifecycle:extensions:1.0.0"
compile "android.arch.persistence.room:runtime:1.0.0-alpha1" annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
//List the entities independent in your database. Separate assorted entities with a comma// @Database(entities = {List.class}, adaptation = 1) //Define an abstruse chic that extends RoomDatabase// public abstruse chic MyDatabase extends RoomDatabase { //Declare your DAO(s) as abstruse methods// accessible abstruse ItemDao itemDao(); }
@Entity //Unless we specify otherwise, the table name will be List// public chic Account { @PrimaryKey //The cavalcade name will be id// clandestine int id; private String item; accessible String getItem() { acknowledgment item; } accessible abandoned setItem(String item) { this.item = item; }
@Entity(tableName = "list")
@ColumnInfo(name = "productName")
@Entity public chic Account { ... ... ... @Ignore Bitmap image; }
@Dao public interface ItemDao { //We don’t charge to address all that Cursor accompanying code; instead, ascertain queries appliance annotation// @Query("SELECT * FROM List") ListfetchAllData(); @Update abandoned update(List list); @Delete abandoned delete(List list); }
@Query("SELECT * FROM List") LiveData> fetchAllData();
implementation "android.arch.persistence.room:rxjava2:1.0.0"
- Lifecycle – An abstruse chic that has an Android Lifecycle absorbed to it. Altar can beam this accompaniment and act accordingly.
- LifecycleOwner – An interface that’s implemented by altar with a Lifecycle. Fragments and Activities already apparatus the LifecycleOwner interface (in Abutment Library 26.1.0 ), and are accordingly LifecycleOwners by default. You can beam LifecycleOwners— and any chic that extends a LifecycleOwner— appliance a LifecycleObsever.
- LifecycleObserver – LifecycleObserver receives updates about LifecycleOwner events. Prior to the Lifecycles library, you could alone acknowledge to methods that were triggered by lifecycle events, like onCreate and onDestroy, but now you can actualize methods that are triggered by changes in a LifecycleOwner’s state. You can accomplish a adjustment lifecycle-aware by abacus the @OnLifecycleEvent annotation.
- Observer – An Observer receives an amend whenever their assigned LifecycleOwner enters a new lifecycle state. An Observer that’s assigned to an Activity will be notified back this Activity enters a paused state, and afresh back it enters a resumed state. You add an Observer to a lifecycle, appliance lifecycle.addObserver(this).
- ON_CREATE.
- ON_DESTROY.
- ON_PAUSE.
- ON_RESUME.
- ON_START.
- ON_STOP .
- ON_ANY.
- INITIALIZED.
- CREATED.
- STARTED.
- RESUMED.
- DESTROYED.
- @Insert. Back you comment a DAO adjustment with @Insert, Room generates an accomplishing inserting all entries into the database in a distinct transaction.
- @Update. Modifies entities in the database.
- @Delete. Removes entities from the database.
- @Query. This is the capital comment you’ll use in your DAO classes and it’s how you’ll accomplish all your read/write operations.
Comments
Post a Comment