Taking Command of Android Development

Get information from a server to a UI.

While this is the most common task we do as mobile devs, Android has not made it easy. Challenges presented by the framework include:

  1. Inability to perform IO (network & disk) operations on main thread
  2. Poor exception handling in built-in threading components (I’m looking at you AsyncTask!)
  3. Clunky instance state management
  4. Boilerplate galore

Working with and displaying network data doesn’t need to be difficult

Luckily for us, as the Android ecosystem has grown and matured, open source solutions that can mitigate all of these problems have started to emerge. This article will introduce you to a few of these libraries and tell you how they’ve changed Android development for me. While I won’t be providing a detailed explanation of how each library works, I will include resources that helped me learn each of them. My hope is to introduce you to a set of tools that I can’t imagine starting a data-driven project without.

Libraries I’ve grown addicted to

  • Retrofit (Networking)
  • Dagger (Dependency Injection)
  • RxJava (Threading, Functional Programming)
  • Butterknife (View Injection)
  • IcePick (View State Persistence)

Patterns that have served me well

Singleton — Both Application-wide and scoped to something like an Activity

Summary of Libraries

Retrofit

Retrofit turns your REST API into a Java interface. http://square.github.io/retrofit/
Retrofit has been able to fulfill every networking demand that I’ve thrown at it. It’s a breeze to add headers, use your own JSON marshaller (GSON is built in), or even pass in your own network client. RxJava support was recently added as well (can return Observables instead of scalar values). Check out how little you need to get started:

RxJava

The next piece of the puzzle is RxJava, which solves issues with threading and exception handling and is fun and concise to boot. RxJava implements the Observer pattern and builds a set of functions on top of emitted values.

@GET(“/search/users”) Observable<UserResponse> users(@Query(“q”) String name)
  1. We ask retrofit for data
  2. We tell it to give us that data on the main thread
  3. We tell it to do the work on an IO thread (which RxJava manages for us)
  4. Finally, we subscribe to the Observable and pass in which methods to call on success and on error.

Dagger

  1. What is scoped to the module with providers for things that cannot be auto injected
  2. What objects act as a starting point to an injection chain (Activities, Views, Tests, etc)

Butterknife

While it has a wide range of functionality, I primarily use Butterknife for view injections:

IcePick

@Icicle int index;@Override public Parcelable onSaveInstanceState() { return Icepick.saveInstanceState(this, super.onSaveInstanceState()); }@Override public void onRestoreInstanceState(Parcelable state) { super.onRestoreInstanceState(Icepick.restoreInstanceState(this, state)); }

RxCommander Pattern

A Commander is a concept that helped me bring everything together (see sample project at end of article for implementation). Think of a Commander as an encapsulation for a particular network endpoint. You need to tell a Commander:

  1. ObjectType for Request
  2. ObjectType for Response
  3. What to call to load a fresh response
  1. Adding rxJava support to any network client
  2. Cacheing network responses in memory
  3. Piggybacking on in-flight requests.
  4. Loading cached or network data (or both!)
Observable<V> fresh(final T request)//new network hitObservable<V> all(final T request) //first cached if available then freshObservable<V> cached(final T request) //cached response onlyObservable<V> get(final T request) //if cache exists return it, otherwise return a fresh response

My typical workflow

Create new retrofit method interface for new endpoint

The Commander Pattern works for me and I hope it will work for you too.

--

--

Google Developer Expert Android, Android at Dropbox nyc

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mike Nakhimovich

Mike Nakhimovich

Google Developer Expert Android, Android at Dropbox nyc