Member-only story

Using SharedPreferences in Android Jetpack Compose

Using SharedPreferences in Android Jetpack Compose: A Step-by-Step Guide

Chintan Joshi
3 min readJul 10, 2023
Jetpack Compose

Introduction:

Android Jetpack Compose is a modern toolkit for building native Android user interfaces. It provides a declarative approach to UI development, making it easier and more efficient to create interactive and dynamic user interfaces. While the recommended way to handle data persistence in Android Jetpack Compose is through the DataStore API, there might be scenarios where you still need to use SharedPreferences. In this article, we will explore how to integrate SharedPreferences into an Android Jetpack Compose project and effectively use it for data persistence.

Step 1: Add Dependencies : First, let’s add the necessary dependencies to your app’s build.gradle file. Open the build.gradle file and include the following line in the dependencies section:

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

This will provide the required dependencies to use SharedPreferences in Android Jetpack Compose.

Step 2: Create a PreferencesManager : Next, let’s create a PreferencesManager class that will encapsulate the read and write operations with SharedPreferences. The class will have methods to…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Chintan Joshi
Chintan Joshi

Written by Chintan Joshi

Android developer. Proficient in Java/Kotlin. Passionate about creating user-friendly apps. Constantly exploring new tech. Open-source contributor.

Responses (1)

Write a response

is it possible to use ApplicationContext so i can init the PreferenceManager class inside viewModel where i can pass the applicationContext that hiltViewModel provides me