Mobile Application
Development
LECTURE NO.13
By Ammara Gillani
Shared Preferences in
Android
Introduction
• Shared Preferences in Android allow storing
and retrieving key-value pairs of primitive data
types. Data is saved in an XML file within the
app's private storage.
• Common data types:
String, int, float, boolean, long.
Characteristics
•- Lightweight data storage for simple data.
•- Stored in an XML file in internal storage.
•- Used for storing user preferences and
settings.
•- Data persists even after app is closed.
•- Easy to implement and use.
Use Cases
•- Remembering user login info
(username/password).
•- Saving app settings (dark mode,
notifications).
•- Storing UI state or form inputs.
•- Tracking onboarding screens completion.
How to use it?
getPreferences() :Used within a single Activity to store and retrieve
small data specific to that activity only, e.g., SharedPreferences
prefs = getPreferences(MODE_PRIVATE); is used when you don’t
need to share the data across other activities.
getSharedPreferences() : Used to store and retrieve data that should
be shared across the whole application, by naming the preference
file explicitly, e.g., SharedPreferences prefs =
getSharedPreferences("UserPrefs", MODE_PRIVATE);, which allows
access from any activity or context in the app.
How to use it?
getDefaultSharedPreferences() : Used to access a standard
shared preference file automatically managed by Android,
especially when using PreferenceFragment or
PreferenceActivity, e.g., SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(context);,
making it ideal for settings or app-wide user preferences.
How to Initialise
getSharedPreferences (String PREFS_NAME,
int mode)
PREF_NAME, You have to write the naem of
the preference
Like “settings”
Modes
MODE_PRIVATE: The default mode, where the created
file can only be accessed by calling application.
MODE_WORLD_READABLE: Creating world-readable
files is very dangerous, and likely to cause security
holes in applications.
MODE_WORLD_WRITEABLE: Creating world-writable
files is very dangerous, and likely to cause security
holes in applications.
Modes
MODE_MULTI_PROCESS: This method will ceck for
modification of preferences even if the Shared
Preference Instance has already been loaded.
MODE_APPEND: This will append the new preferences
with the already existing preferences.
MODE_ENABLE_WRITE_AHEAD_LOGGING: Database
open flag. When it is set, it would enable write ahead
logging by default.
Storing Data
SharedPreferences pref =
getApplicationContext().getSharedPreferences(“settings”,
MODE_PRIVATE);
For Private Mode
Editor editor = pref.edit();
editor.putBoolean(“key_name”, true); // storing boolean - true/ false
editor.putString(“key_name”, “string value”); // storing string
editor.putInt(“key_name”, “int value”); // storing integer
editor.putFloat(“key_name”, “float value”); // storing float
editor.putLong(“key_name”, “long value”); // storing long
editor.commit( ); // commit changes
Retrieving Data
pref.getString(“key_name”, null)// getting String
pref.getInt(“key_name”, null)// gettingInteger
pref.getFloat(“key_name”, null)// getting Float
pref.getLong(“key_name”, null)// getting Long
pref.getBoolean(“key_name”, null)// getting Boolean
Implementations
UI Design
1- Enter 2- Re-open app 3- Already
perefences Shared
Preferences