Room + Kotlin Flow — The modern Android architecture for location-aware apps

The first Android demo video from Google

How to handle location updates?

  1. Be able to listen for location updates in one place in the app (“single source of truth”) and reflect the results in multiple activities and fragments
  2. Be able to listen for location updates when the app isn’t visible to the user (e.g., to show updates in a notification and/or log to files while the user is looking at a different app)

Almost there…

The “event bus” design of LocalBroadcastManager is deprecated because it violates Android architecture principles

The solution — Room + Kotlin Flow

Our UI can observe changes from the repository (Room) using Kotlin Flow
  1. Implement the Room database to persist location data
  2. Modify the Service to store location updates in Room
  3. Modify the Activity to observe changes in Room via Flow

Room implementation 🏠

Hilt implementation for dependency injection 🔪

Service

Activity

Gradle configuration 🔧

Putting it all together

The Service inserts new locations into Room, and the Activity observes them via Kotlin Flow!

Closing thoughts

--

--

--

Improving the world, one byte at a time. @sjbarbeau, https://github.com/barbeau, https://www.linkedin.com/in/seanbarbeau/. I work @CUTRUSF. Posts are my own.

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

Recommended from Medium

Google play woes and the Android dev community

Things I Got Stuck On Developing BLE App on Android

Monitoring Wi-Fi Connectivity Status in a post nougat world — Part 1

Animated Opacity in Flutter

Creating a Custom Button Using PorterDuff.Mode

Publishing an Android Application with Flutter to the Google Play Store

Creating reliable tests for Android notifications

White lighthouse on rocky seashore

Using MySQL Database in Android Apps

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
Sean Barbeau

Sean Barbeau

Improving the world, one byte at a time. @sjbarbeau, https://github.com/barbeau, https://www.linkedin.com/in/seanbarbeau/. I work @CUTRUSF. Posts are my own.

More from Medium

Solved — Unable to load class AndroidComponentsExtension after upgrading the Android Gradle Plugin…

Integration of Huawei Account Kit in Book Reading Android app (Kotlin)

Android CodeView: Auto Indentation, Find and replace

Simplify Default Android Release Notes on Fastlane