0
0
Android Kotlinmobile~3 mins

Why SharedPreferences / DataStore in Android Kotlin? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a tiny piece of code can make your app feel like it truly knows its user!

The Scenario

Imagine you want your app to remember a user's theme choice or login status every time they open it. Without a simple way to save this small data, you'd have to ask the user again and again, which feels frustrating and unprofessional.

The Problem

Manually saving data by writing files or databases for tiny settings is slow and complicated. It can cause bugs, data loss, or slow app startup. Plus, it wastes time writing extra code for simple tasks.

The Solution

SharedPreferences and DataStore provide easy, fast, and reliable ways to save small pieces of data like settings or user preferences. They handle storage behind the scenes so you can focus on your app's features.

Before vs After
Before
val file = File(context.filesDir, "settings.txt")
file.writeText("dark_mode=true")
After
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
prefs.edit().putBoolean("dark_mode", true).apply()
What It Enables

It lets your app remember user choices smoothly, creating a friendly and personalized experience every time.

Real Life Example

When you open a news app and it remembers you prefer dark mode and your favorite topics, that's SharedPreferences or DataStore working quietly in the background.

Key Takeaways

Saving small data manually is slow and error-prone.

SharedPreferences/DataStore simplify storing user settings.

This makes apps feel personal and easy to use.