https://github.com/nicosnicolaou16/data-store-setup
This project demonstrates the setup of the Jetpack Library's Preference Data Store.
https://github.com/nicosnicolaou16/data-store-setup
android android-application data-store datastore-android
Last synced: 2 months ago
JSON representation
This project demonstrates the setup of the Jetpack Library's Preference Data Store.
- Host: GitHub
- URL: https://github.com/nicosnicolaou16/data-store-setup
- Owner: NicosNicolaou16
- Created: 2024-03-08T23:34:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-02-27T23:09:05.000Z (4 months ago)
- Last Synced: 2026-02-28T03:34:31.672Z (4 months ago)
- Topics: android, android-application, data-store, datastore-android
- Language: Kotlin
- Homepage: https://medium.com/@nicosnicolaou/preferences-data-store-setup-b197e3db09dd
- Size: 148 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Data Store Setup
[](https://linktr.ee/nicos_nicolaou)
[](https://nicosnicolaou16.github.io/)
[](https://twitter.com/nicolaou_nicos)
[](https://linkedin.com/in/nicos-nicolaou-a16720aa)
[](https://medium.com/@nicosnicolaou)
[](https://androiddev.social/@nicolaou_nicos)
[](https://bsky.app/profile/nicolaounicos.bsky.social)
[](https://dev.to/nicosnicolaou16)
[](https://www.youtube.com/@nicosnicolaou16)
[](https://g.dev/nicolaou_nicos)
A comprehensive guide and implementation example for setting up **Jetpack Preference DataStore** in Android. This project demonstrates how to handle persistent data using a modern, type-safe, and asynchronous approach.
> [!IMPORTANT]
> Check out the full deep-dive article for this setup:
> 👉 **[Preferences Data Store Setup - Medium](https://medium.com/@nicosnicolaou/preferences-data-store-setup-b197e3db09dd)** 👈
## 🛠️ Implementation Guide
### Step 1:
Add the dependencies library
#### Kotlin
```Kotlin
val preferencesDataStoreVersion by extra("1.2.0")
dependencies {
//...
//Preferences dataStore
implementation("androidx.datastore:datastore-preferences:$preferencesDataStoreVersion")
}
```
#### libs.versions.toml
```toml
[versions]
# other versions here...
dataStore = "1.2.0"
[libraries]
# other libraries here...
preference-data-store = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "dataStore" }
```
#### Application Gradle:
```Kotlin
implementation(libs.preference.data.store)
```
### Step 2:
Create a Helper class that initialize the Data Store
```Kotlin
object PreferencesDataStoreHelper {
private const val PREFERENCES_DATA_STORE_NAME = "preferences_data_store_name"
private val Context.dataStore: DataStore by preferencesDataStore(name = PREFERENCES_DATA_STORE_NAME)
//Other Code Here...
}
```
### Step 3:
Create the two methods for save and print and for our example we are using only for String value
```Kotlin
object PreferencesDataStoreHelper {
//Other Code Here...
internal suspend fun saveStringValue(
value: String,
key: Preferences.Key,
context: Context
) {
context.dataStore.edit { saveData ->
saveData[key] = value
}
}
internal fun getStringValueFlow(key: Preferences.Key, context: Context): Flow =
context.dataStore.data
.catch { exception ->
when (exception) {
is IOException -> emit(emptyPreferences())
else -> throw exception
}
}.map { readData ->
readData[key]
}
//Other Code Here...
}
```
### Step 4:
Bonus Part - delete specific value and delete all Data Store values
```Kotlin
object PreferencesDataStoreHelper {
//Other Code Here...
internal suspend fun removeStringValueWithSpecificKey(
key: Preferences.Key,
context: Context
) {
context.dataStore.edit { it.remove(key) }
}
internal suspend fun removeAllValues(context: Context) {
context.dataStore.edit { it.clear() }
}
}
```
### Step 5:
Call the methods for save, print, delete specific value and all values in the screen
```Kotlin
class MainActivity : ComponentActivity() {
//Other Code Here...
@Composable
fun DataStoreMainView() {
val scope = rememberCoroutineScope()
val context = LocalContext.current
Box(contentAlignment = Alignment.Center) {
Column {
Button(
onClick = {
scope.launch {
/**
* Save the Value
* */
PreferencesDataStoreHelper.saveStringValue(
"testValue",
stringPreferencesKey(PREFERENCE_STRING_KEY),
context
)
}
},
modifier = Modifier.width(170.dp)
) {
Text(stringResource(R.string.save_string_value))
}
Button(
onClick = {
scope.launch {
/**
* Read the Value
* */
PreferencesDataStoreHelper.getStringValueFlow(
stringPreferencesKey(PREFERENCE_STRING_KEY),
context
).collect {
if (it != null) {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
}
}
},
modifier = Modifier.width(170.dp)
) {
Text(stringResource(R.string.print_string_value))
}
Button(
onClick = {
scope.launch {
/**
* Remove a Specific Value
* */
PreferencesDataStoreHelper.removeStringValueWithSpecificKey(
stringPreferencesKey(PREFERENCE_STRING_KEY),
context
)
}
},
modifier = Modifier.width(170.dp)
) {
Text(stringResource(R.string.remove_specific_value))
}
Button(
onClick = {
scope.launch {
/**
* Remove all the Values
* */
PreferencesDataStoreHelper.removeAllValues(
context
)
}
},
modifier = Modifier.width(170.dp)
) {
Text(stringResource(R.string.remove_all_values))
}
}
}
}
}
```
## 🔧 Versioning
- **Data Store Version:** **1.2.0**
- **Target SDK:** **36**
- **Minimum SDK:** **29**
- **Kotlin Version:** **2.3.10**
- **Gradle Version:** **9.0.1**
## 📚 References & Resources
- [Android Jetpack DataStore](https://developer.android.com/topic/libraries/architecture/datastore)
## ⭐ Stargazers
If you enjoy this project, please give it a star!
Check out all the stargazers
here: [Stargazers on GitHub](https://github.com/NicosNicolaou16/Data-Store-Setup/stargazers)
## 🙏 Support & Contributions
This project is actively maintained. Feedback, bug reports, and feature requests are welcome! Please feel free to **open an issue** or submit a **pull request**.