The following is the first few sections of a chapter from Exploring Android, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Publishing LiveData

Part of the architecture that we are moving towards involves our fragments being delivered ViewState objects, representing what it is that they are supposed to be displaying to the user. This means that somehow, something is pushing ViewState objects over to the fragments.

The RosterViewModel would seem to be a candidate, since our three fragments have access to that object. However, one of the tenets of view-models in GUI architectures is that while the UI (“view”) knows about the view-model, the reverse is not true. A view-model should not hold direct references to the views, which means that RosterViewModel should not have fields for RosterListFragment, DisplayFragment, and EditFragment. And, as a result, RosterViewModel has no good way to call a method on those fragments to say “hey, here is the latest ViewState!”.

Instead, we turn to reactive programming.

There are frameworks set up to serve as event-driven pipelines for data, ones where a stream objects can flow from producers to consumers. The most powerful of these available for Android developers is called RxJava, and we will be using RxJava coming up in later tutorials. However, while RxJava is available for Android developers, RxJava is a pure-Java library, and it knows little about Android. In particular, it knows nothing about configuration changes and how that affects our UI.

The Architecture Components has its own reactive programming framework: LiveData. This is much smaller than RxJava, and it is less capable as a result. However, it is aware of configuration changes, which makes it useful for us when we want to deliver events to activities and fragments. LiveData can be used directly or as a bridge from RxJava into our activities and fragments.

So, in this tutorial, we will set up LiveData in our RosterViewModel to deliver ViewState objects to our fragments. Right now, we will do that “by hand”, while we continue building up our GUI architecture. Later, we will tie LiveData to RxJava, as RxJava will be handling our event flows through the rest of the app.

This is a continuation of the work we did in the previous tutorial. The book’s GitHub repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Read Me!

Coverage of LiveData from the Architecture Components can be found in:

Step #1: Holding a MutableLiveData

The preview of this section was traded for a bag of magic beans.

Step #2: Publishing a ViewState

The preview of this section may contain nuts.

Step #3: Rendering a ViewState

The preview of this section was last seen in the Bermuda Triangle.

Step #4: Observing the Stream

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Step #5: Pondering What We Are Missing

The preview of this section is sleeping in.

What We Changed

The preview of this section is sleeping in.