Android Gets Architecture Components
Written by Mike James   
Wednesday, 31 May 2017

Is it a sign of maturity when a system gets its own architecture components? It all depends on how you look at it. 

androidapp

 

Architecture components seem to occur when the developers of a system have enough time to attempt to abstract the bigger, higher level, tasks that nearly every application has to implement. It seems that the time has arrived for Android. The preview of the Architecture Components was announced at Google I/O, but it mostly went unnoticed. 

In this preview two issues are addressed: 

  • Automatically manage your activity and fragment lifecycles to avoid memory and resource leaks

  • Persist Java data objects to an SQLite database 

The big problem with most component libraries that have the word "architecture" in their title is working out what exactly they do. For example:

"New lifecycle-aware components provide constructs to tie core components of your applications to lifecycle events, removing explicit dependency paths."

If you already know what that means you are probably wondering why anyone would have trouble with it. The problem is that Android state management is ridiculously complicated and you don't have to look far to find quite high profile apps that get it wrong.

In an ideal world the whole life-cycle issue would be swept under the carpet by the operating system. There isn't much excuse for the operating system not taking care of it all and leaving the app programmer with the fiction that their app is the only one running and never gets booted out. After all, its how the desktop has worked for a long while and mobile hardware gets more powerful everyday. 

Look at what is on offer more carefully. What we seem to have is an observer class that makes working with the different lifecycle states a little easier at the cost of learning yet another layer of software. It allows you to avoid having to code what happens to your component in the relevant event handlers of the Activity or Fragment. Instead your component can simply observe the state changes and have its own "event handlers" called automatically. 

You can use this to build lifecycle-aware components. For example, there is the ViewModel class which stores UI-related data so that it survives state changes such as screen rotation. This is basically a more sophisticated solution to the problem than the basic onSaveInstanceState and similar. The ViewModel outlives the Activity and Fragment and so provides persistent storage. When the Activity/Fragment is recreated it gets the same ViewModel instance. There is also a LiveData class that will basically only keep data fresh when there is an active observer, i.e. when some component is running and making use of the data. 

The second objective is to provide a more general data persistence:

The Android Architecture Components also simplify data persistence with the Room library. Room provides an object-mapping abstraction layer that allows fluent database access while harnessing the full power of SQLite. 

The idea is that you annotate a Java data object, create a database for them, and define a DAO class with the necessary SQL to access and modify the database:

room

 

You can find out more from the promo video and there is lots of documentation - which isn't that easy to follow:

 

 

So will I be using the architecture components?

No.

The reason is that it is only in preview and subject to change, possibly big changes. I plan to keep an eye on it and see what happens. 

There is also the small matter that it doesn't provide any databinding and doesn't offer any help in using it with the databinding library, which is a potentially a much more important development in Android programming - even if it does seem to be falling by the wayside. 

Then, of course, there is Kotlin, which changes everything. 

You could use the architecture components from Kotlin, but probably would be easier and better to wait for the Kotlin version - if there ever is one.

With turbulent times in the Android world, now is probably not the moment for an architecture library and certainly not an under-developed and probably under-resourced one. 

 

androidapp

 

More Information

Android and Architecture

Android Architecture Components

Related Articles

Android Studio 3

Kotlin - New Language For Android

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


Java Version 22 Released
04/04/2024

JDK 22 is not a Long Term Support release, but is one of the regular releases that are scheduled to arrive every six months. Still, it has got a lot to show for itself.



We Built A Software Engineer
20/03/2024

One of the most worrying things about being a programmer today is the threat from AI. It has gone so far that NVIDA CEO Jensen Huang proclaims that you really shouldn't start training as a programmer  [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

 

Last Updated ( Wednesday, 31 May 2017 )