What’s New in Android : A technical approach

Harun Wangereka
6 min readMay 19, 2019

--

After Google I/O 19, there are tons of new updates for the android development community. In this article, we will take a look at some of the new updates that were released, especially for the android architecture components and Android Jetpack. For the Jetpack libraries, it will be from now on coroutines first, that is they are made with kotlin coroutines in mind and some libraries are already taking advantage of the coroutines for example: workmanager,viewmodels etc.

View Binding

This feature will be available in Android Studio 3.6. It aims to replace the old findViewById and other view binding methods like butter knife and kotlin synthetics. With view binding, the views in your XML layout will be accessed as follows:

The following are the advantages of View Binding:

  1. Binding classes built by the gradle plugin
  2. 100% compile time safety
  3. Full Android Studio integration
  4. Compatible with data binding
  5. Incremental Compilation
  6. Refactoring Support
  7. Better error messaging

Room

Room 2.1 comes with a couple of new exiting features. One of them being that we will now be able to use the suspending methods and Room will generate correct code including using a background dispatcher. You can have suspending methods which do transaction also.

Another interesting feature that was added was Full Text Search which will make text search more efficient. In Room 2.0 when say you want to do a search for a song in a music application, you had to write the following:

This would result to large statements on your query, but in room 2.1 it has been made to be a lot easier. You just annotate your entity class with fts4 annotation as shown below

And your query now becomes this :

This is more concise and more efficient.Amazing, isn’t it? And yet we still have more additions to room. They announced Database Views. Database Views are like tables but they are different. You cannot insert data into views. They are kinda like queries with a different name. To have a database view, you just annotate your data class with the database view annotation.

Lets say you want to display a list of albums with aggregated information like total time played, this is what the data class would look like:

The good thing is now we can use this view as if it was another table.

They also expanded RX support in room, now you can have insert, update and delete method that return Completable, Maybe and Single. You also use Rx as return type for Query tag that performs write statements such as insert update or delete operation.

Whats Next for Room?

  1. Incremental Annotation Processor to improve on build speeds
  2. Relationship improvements
  3. Migrations improvements
  4. Coroutines Channels and Flow

Navigation Components

There are a couple of new things that were announced for navigation. One being that you can now scope viewmodels to navigation graph for example sign up flow can have scoped view models. The scope sits between activity and single destination. The following shows an example of on boarding process with a viewmodel scoped to the nav graph

In our view model we have two methods for setting the username and getting the username. Lets see how to use this two methods in our fragments.

In the SetupFragment we call the setUsername(username) method with the username value.

In the Profile Fragment we call the getUsername method which will return the username. Notice that we are using Kotlin’s Property Delegation to initialize our view model and scope it to the navigation graph.

Other key updates were :

  1. Navigation by URI
  2. Conditional Navigation Docs improvements- before it was a struggle trying to get conditional navigation work but the navigation component team has released the docs to show us how to properly implement conditional navigation
  3. Dialog destinations — Replaces the show() methods and adds dialogs to contribute to the navigation graph

Single Activity Architecture ensures that even you are deep in one fragment you can still call main navigation controller and go to whichever fragment you want.You can also link multiple nav graphs using </include/> in your main nav graph.

We also have Safe Args for the navigation component. Safe args moves check to compile time,rely on autocomplete to only give valid actions,are type safe arguments and support more types like Parcelables, Serializable and enums.

There will be better support for dynamic features modules in the future.

Lifecycles

A way to observe lifecyle events of fragments and activities. Activity and Fragments have lifecycle scopes. We have the ViewModel which retains state across configuration changes. We also have LiveData which are lifecycle-aware observables. Lifecycle scope is important for coroutines to avoid cancellation of jobs.

Another addition to the lifecycle scope is launchWhenStarted for dealing with fragment transactions as nothing guarantees that fragment transaction is complete, when not complete, it will bring the Illegal State Exception

ViewModel

ViewModel manages UI related data in Activity lifecycle. It allows to survive configuration changes in the App(like Screen rotation).There was a bunch of stuff introduced in the ViewModel. One of them being the viewModelScope for using with kotlin coroutines. Previously this was the implementation:

With the viewModelScope we have all these done for us under the hood. This is how we will be using our viewModelScope

So, as part of Jetpack Google launced something called Saved State which helps us to store and retrieve the data from saved state after it goes through the System Initiated Process Death. This is how we will implement the savedstate for our viewmodel

Saved stated(map-like object) — works together with viewmodel .You have to add dependancy in order for you to use it.

Saved State is not you database . No magic restrictions, the restrictions are :
1 .Parcelable data
2. Limited data size

Saved State of ViewModel can be considered as a replacement for onSaveInstanceState() as it can also store the data because UI data is always referenced from ViewModel of Architecture Components and not the View (Activity/Fragment). So to use onSaveInstanceState() will require us to do some coding.

Retrofit

Has the support for suspend function but this is not yet available in the stable release. Version 2.5.1-SNAPSHOT of the library introduced suspending functions into the Retrofit world. You can now write a retrofit service with suspending functions that returns directly your response body type.

Also with the CoroutineCallAdapterFactory , Retrofit allows us to return kotlin coroutine’s Deferred as the return type of our service functions.

LiveData

Starting from lifecycle:2.2.0-alpha01 , LiveData now has a builder that yields the result of a computation as a LiveData .

Kotlin Ideomatic Code

We now have lambda block support for LiveData observations.

For LiveData Transformations,no need to use static methods on transformations. Instead now we have extension functions:

For ViewModel Initialization, no need to use lateinit var for Initialization. Instead we now use property delegation

Testing

There is a testing library kotlinx-test-coroutines for testing kotlin coroutines.

A big thanks goes to Apps:Lab team , Android 254 and Kotlin Kenya teams for the guidance and support.

--

--

Harun Wangereka

Google Developer Expert for Android | Android Engineer | Co-organizer droidconKE, Android254 & Kotlin Kenya | Android Author @raywenderlich.com