How to store and retrieve data using the Android DataStore

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “DataStore.”

A step by step guide on saving and gathering local data (preferences) from a users device using Android Studio and Kotlin.

Step One: Add the Dependencies

A screenshot of Android Studio showing the app level build.gradle.kts file. Highlighted is the addition of the dependencies required for the DataStore to work.

In the projects app level build.gradle.kts, add the following dependencies.

implementation("androidx.datastore:datastore-preferences:1.0.0")

implementation("androidx.datastore:datastore-preferences-rxjava2:1.0.0")

implementation("androidx.datastore:datastore-preferences-rxjava3:1.0.0")

Please note that versions displayed above may not be the latest versions, please update the code above to the latest version in your Android Studio project.

Step Two: Sync the Gradles

A screenshot of Android Studio showing a gradle file that has recently been modified. Highlighted is the "Sync Now" button that appears on the right of the prompt at the top of the Gradle file. Click this button to sync the gradle files with the project.

Press Sync Now in the bar above the Gradle to update the project dependencies.

Step Three: Create the Keys

A screenshot of Android Studio showing the PreferencesKeys.kt file. We have created a file that holds an object with a value for each DataStore preference.

In your project, create a PreferenceKeys.kt file and create keys for each variable that you wish to store.

Step Four: Create the DataCoordinator files

A screenshot of Android Studio with the DataCoordinator.kt,DataCoordinator+DataStore.kt and the DataCoordinator+Update.kt file highlighted.

In the project, create 3 files called DataCoordinator.kt, DataCoordinator+DataStore.kt and DataCoordinator+Update.kt.

These files will allow us to create a singleton that manages the gathering, storing and offering of variables across the app.

Step Five: Setup the DataCoordinator

A screenshot of Android Studio showing the DataCoordinator.kt file with the code below.

Setup the DataCoordinator.kt as a singleton and add the necessary variables and default values for each of parameters that you wish to store in the DataStore.

Please note that a variable and default should be created per key created in Step Three.

Step Six: Create the DataStore Set and Get Functionality

A screenshot of Android Studio showing the DataCoordinator+DataStore.kt file with the code that's available below.

In the DataCoordinator+DataStore.kt file, create functions to save and retrieve each of the variables.

As demonstrated in the code below, the DataStore may not find the relevant variable, and requires a default value to be returned in the event it cannot find it. Please use the ones that were defined in the prior step.

Step Seven: Create the DataCoordinator Update Functionality

A screenshot of Android Studio showing the DataCoordinator+Update.kt file with the code that’s available below.

Complete the DataCoordinator's Update extension by creating functions that update each preference's variable and datastore variable.

This is done to create a unified way to update variables from anywhere in your app.

Step Eight: Complete the DataCoordinator Initialization Functionality

A screenshot of Android Studio showing the DataCoordinator.kt file with the updated initialize function. The code is available below.

Complete the initialize function of the DataCoordinator to gather the variables on initialize using code similar to that below.

Step Nine: Initialize the DataCoordinator

A screenshot of Android Studio showing the MainActivity.kt file. Highlighted is where we are initializing the DataCoordinator. This can happen wherever you wish but we suggest you do so in a setupCoordinators() function.

Initialize the DataCoordinator in the MainActivity.

Step Ten: Test

A screenshot of Android Studio showing the emulator. Highlighted in the logs is that the DataCoordinator has been initialized correctly.

Run the app and confirm that the DataCoordinator works and loads the defaults.

Step Eleven: Update a DataStore variable

A screenshot of Android Studio showing the MainActivity.kt file. Highlighted is where we are updating a DataStore variable by calling the DataCoordinator. This can happen wherever you wish and is intended to serve to test that the code works.

Call one of the update functions that you declared in Step Seven to allow you to test that the updates are working.

Please note that as we are using a Singleton, you can call this function from anywhere in the app.

Step Twelve: Test the Update

A screenshot of Android Studio showing the emulator in landscape. Highlighted in the logs is that the DataCoordinator update has gone through correctly.

Run the app and perform the action required for the update. Then change orientation and confirm that the update went through.

Please note that we allow for configuration changes, meaning that every time that the app changes orientation, the app will load the variables from the DataStore.

How do I reset the DataStore or a variable ?

If you wish to reset the DataStore variable, you must either set each value to its default programmatically or delete the app and reinstall it.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details