ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Using Android Jetpack Datastore in Jetpack Compose

--

In this article, we will learn about Jetpack Datastore which is a new data storage solution in Android and how to use it in a Compose Application.

Photo by Christian Wiediger on Unsplash

According to Google:

Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.

Jetpack Datastore is one of the awesome libraries that have being introduced into the growing list of Jetpack Libraries and according to them

Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers. DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally

And it is a replacement for our dear old SharedPreferences and there’s a great differences between them.

DataStore Vs SharedPreferences

Image from Android developer blog

Types of saving data in DataStore

  • Preference DataStore, like SharedPreferences, has no way to define a schema or to ensure that keys are accessed with the correct type.
  • Proto DataStore lets you define a schema using Protocol buffers. Using Protobufs allows persisting strongly typed data. They are faster, smaller, simpler, and less ambiguous than XML and other similar data formats. While Proto DataStore requires you to learn a new serialization mechanism, we believe that the strongly typed schema advantage brought by Proto DataStore is worth it.

Using DataStore in Our app

Add the required dependencies to your build file:

After importing the right dependencies, we move the next step.

Saving Data in DataStore

Let’s say we want to our user’s email when logging in, in case we need later in the app. We will create a Kotlin class called SaveUserEmail and this class will be used to handle all the saving and retrieving of our data.

  • The SaveUserEmail class will require the Context object to create ourDataStore.
  • We then create our DataStore with the preferencesDataStore using extension function of the Context object. We need to set the name of our datastore with the compulsory name parameter.
  • We need to define a key for each String value to save with the stringPreferenceesKey() function. You also need to define the name of the key with name parameter (this is available for other primitive values too).
  • We can create a function to save the email in the DataStore. The DataStore provides a edit() method to save the value. We can pass the key and value to the MutablePreferences in the trailing lambda. Note that the edit() function is a suspend function ( which needs to run in a Coroutine Scope).
  • Now you can get the saved preferences from the datastore with datastore.data which returns a Flow<Preferences> and with the .map{} operator we can can get the Flow<String> which is our email by passing the right key into the preferences.

Now we head to our composable and do the necessary stuff:

In order to get the saved data well collect it from flow like this:

After running this we show get the desired result.

And the whole code looks like this:

Thank you for reading! If this helped in anyway I'll love a clap from you.

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Raheem Jr.

Native Android developer | Mobile Developer, I write about thoughts and problems I solve and help other developers avoid them.

Responses (1)