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

How to add In-App Review to flutter application

Day66 of #100DaysOfCode

First steps with Glance (App Widgets)

CI / CD at SwiftKey (Part 2)

Android build system

Android Interviews: Take-home assessment tests

picture of AS from https://developer.android.com/studio

Day69 of #100DaysOfCode

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

Kotlin: Extensions to functions

Using Jetpack Compose in XML/View and Vice Versa : Perfect Interoperability🧁

Retained Dagger Component over Configuration Changes

Parallel Multiple API Calls or Network Calls Using Kotlin Coroutines