Shared Preferences is a simple and powerful storage system in Android used to save small amounts of data in key-value format. It is commonly used to store user settings, login states, app preferences, and simple configuration data that must persist even after the app is closed.
Almost every Android application uses Shared Preferences for lightweight local storage.
What are Shared Preferences?
Shared Preferences is a persistent storage mechanism in Android that allows you to save primitive data types like:
- String
- Integer
- Boolean
- Float
- Long
Data is stored in XML files inside the app’s private storage.
It works in key-value pairs, where each value is stored with a unique key.
Why Use Shared Preferences?
Shared Preferences is important because it:
- Stores small data locally
- Keeps data after app restart
- Saves user settings
- Maintains login sessions
- Improves user experience
- Is easy to implement
It is not suitable for large or complex data, but it is perfect for simple app preferences.
Where is Shared Preferences Used?
Shared Preferences is commonly used in:
- Login and logout systems
- Dark mode settings
- Language selection
- User onboarding screens
- Remember me functionality
- App theme settings
- User session management
Almost every modern Android app uses it in some form.
How Shared Preferences Works
Shared Preferences works in three main steps:
- Create a SharedPreferences object
- Store data using an editor
- Retrieve data using keys
Each value is saved using a unique key that can be accessed later.
Creating Shared Preferences
To create Shared Preferences:
SharedPreferences sharedPreferences =
getSharedPreferences(
"MyAppPrefs",
MODE_PRIVATE);
Here:
"MyAppPrefs"is the file nameMODE_PRIVATEmeans only this app can access it
Saving Data in Shared Preferences
To store data, use the editor:
Example
SharedPreferences sharedPreferences =
getSharedPreferences(
"MyAppPrefs",
MODE_PRIVATE);
SharedPreferences.Editor editor =
sharedPreferences.edit();
editor.putString(
"username",
"Ali");
editor.putInt(
"age",
22);
editor.putBoolean(
"isLoggedIn",
true);
editor.apply();
Key Methods for Saving Data
Shared Preferences supports different data types:
putString()
putInt()
putBoolean()
putFloat()
putLong()
Each value is stored with a key.
Retrieving Data from Shared Preferences
To read saved data:
SharedPreferences sharedPreferences =
getSharedPreferences(
"MyAppPrefs",
MODE_PRIVATE);
String username =
sharedPreferences.getString(
"username",
"default");
int age =
sharedPreferences.getInt(
"age",
0);
boolean isLoggedIn =
sharedPreferences.getBoolean(
"isLoggedIn",
false);
If the key does not exist, the default value is returned.
Example: Login System Using Shared Preferences
Saving Login State
SharedPreferences sharedPreferences =
getSharedPreferences(
"LoginPrefs",
MODE_PRIVATE);
SharedPreferences.Editor editor =
sharedPreferences.edit();
editor.putBoolean(
"loggedIn",
true);
editor.putString(
"username",
"Ali");
editor.apply();
Checking Login State
SharedPreferences sharedPreferences =
getSharedPreferences(
"LoginPrefs",
MODE_PRIVATE);
boolean loggedIn =
sharedPreferences.getBoolean(
"loggedIn",
false);
if(loggedIn) {
System.out.println(
"User is logged in");
} else {
System.out.println(
"User is not logged in");
}
This is widely used in authentication systems.
Updating Data in Shared Preferences
To update a value, simply overwrite the key:
editor.putString(
"username",
"Ahmed");
editor.apply();
The old value is replaced automatically.
Removing Data
To remove a specific value:
editor.remove("username");
editor.apply();
To clear all data:
editor.clear();
editor.apply();
Modes in Shared Preferences
Common modes include:
MODE_PRIVATE
Only the app can access the data.
MODE_WORLD_READABLE (deprecated)
Previously allowed other apps to read data.
MODE_WORLD_WRITEABLE (deprecated)
Previously allowed other apps to write data.
Modern Android uses MODE_PRIVATE only.
Example: Remember Me Feature
Saving User Choice
editor.putBoolean(
"rememberMe",
true);
editor.apply();
Checking Value
boolean rememberMe =
sharedPreferences.getBoolean(
"rememberMe",
false);
This is commonly used in login screens.
Example: Dark Mode Setting
Saving Theme Preference
editor.putBoolean(
"darkMode",
true);
editor.apply();
Retrieving Theme
boolean darkMode =
sharedPreferences.getBoolean(
"darkMode",
false);
Apps use this to maintain UI themes.
Shared Preferences in Activity Lifecycle
Shared Preferences data remains available even after:
- App restart
- Activity restart
- Device reboot
This makes it ideal for persistent settings.
Advantages of Shared Preferences
Shared Preferences offers several benefits:
- Easy to use
- Lightweight storage
- Fast access
- Persistent data
- No database required
- Ideal for small data
It is one of the simplest storage solutions in Android.
Limitations of Shared Preferences
Shared Preferences is not suitable for:
- Large data storage
- Complex structured data
- Media files
- Relational data
- High-performance databases
For large data, use Room Database or Firebase.
Real-World Applications
Shared Preferences is used in:
- Login systems
- App settings storage
- User preferences
- Theme management
- Language selection
- Onboarding flow control
- Session management
- Notification settings
Almost every Android app uses it behind the scenes.
Common Beginner Mistakes
Forgetting apply() or commit()
Incorrect:
editor.putString("name", "Ali");
Correct:
editor.putString("name", "Ali");
editor.apply();
Using Wrong Keys
Keys must match exactly when saving and retrieving data.
Overusing Shared Preferences
Do not store large or complex data.
Not Providing Default Values
Always provide fallback values:
getString("key", "default")
Best Practices
When using Shared Preferences:
- Use meaningful keys
- Group related preferences
- Avoid storing sensitive data
- Use constants for keys
- Keep data small and simple
- Clear unused preferences
These practices improve maintainability and security.
Importance of Shared Preferences
Shared Preferences is important because it:
- Stores user settings permanently
- Maintains app state
- Improves user experience
- Supports authentication systems
- Enables personalization
- Simplifies local storage
It is a fundamental tool in Android development.
Conclusion
Shared Preferences is a lightweight storage system used in Android to save small pieces of data in key-value format. It is ideal for storing user preferences, login states, and simple configuration settings that must persist across app sessions. With its simplicity and efficiency, Shared Preferences plays a key role in building modern Android applications that remember user data and provide a smooth, personalized experience.