How can we use LiveData to implement Navigation in our Android apps? Let’s explore some options.

Anton Spaans
The Kotlin Chronicle
12 min readApr 23, 2019

--

LiveData and ViewModels from the Android Architecture Component library are building blocks of the reactive MVVM (Model-View-ViewModel) and MVI (Model-View-Intent) patterns when showing content in our app’s UI. But what about navigation? There are some issues we’d need to address and this article aims to help us with that.

What is the problem?

The problem revolves around the LiveData component. Whether we use the MVVM or MVI pattern, or some variant of them, our UI is reactive and it observes emitted values from one or more LiveData properties of a ViewModel. Any change in these properties will cause the UI, or some part of it, to be updated.

Right after the UI re-subscribes to a LiveData property, after a rotation or another configuration change, the last value of the property is re-emitted. This makes sure that the data on the screen is shown again after the UI-component has been recreated. All good, so far!

However, if the LiveData property represents a navigation event, we do not want this event to be emitted again after a configuration change. We do not want to navigate to the same screen again after the user has rotated the phone and then hits the Back button. Events are one-shot values…

--

--

Anton Spaans
The Kotlin Chronicle

Associate Director of Product Engineering at @AccentureSong. You can find me online @streetsofboston or at https://www.linkedin.com/in/antonspaans/