04.1 Data Persistence - Preferences
04.1 Data Persistence - Preferences
Preferences
Table of Content
Introduction
Shared Preferences
2
Introduction
Why storing and retrieving data in mobile applications is important ?
- User Experience
- Offline Functionality
- Data Integrity
- Personalization
- Performance Optimization
- Data Backup and Restoration
- Cross-Device Synchronization
3
Introduction (Cont.)
Data persistence in Android refers to the ability to store and retrieve data beyond the
lifetime of an application.
It allows the app to save and access data even after the app is closed or the device is
restarted.
4
Introduction (Cont.)
Android provides various mechanisms for data persistence, including:
● Shared Preferences
● Internal Storage
● External Storage
● SQLite Database
● Content Providers
● Network and Cloud-Based Storage
5
Introduction (Cont.)
Different data persistence mechanisms have their own strengths and use cases.
- Type of data,
- Data size,
- Security requirements
- App's specific needs.
6
What is Data Persistence?
Data Persistence allows users to save their settings, preferences, and progress.
For E.g.:
7
Shared Preferences
● Shared Preferences is a lightweight data storage mechanism
● It allows applications to store and retrieve key-value pairs persistently.
● It is simple & convenient to store small amount of data
● Data is stored in XML file in the directory
data/data/<package-name>/shared-prefs
8
Shared Preferences (Cont.)
Key features of Shared Preferences:
● Key-Value Storage
● Application-Specific Storage
● Persistent Storage
● XML-Based Storage
● Easy API Access
● Lightweight and Efficient
9
Shared Preferences (Cont.)
● We can save the preferences data either in single or multiple files based on our
requirements.
● To get values from the shared preferences we use:
- getPreferences() method (for single file)
- getSharedPreferences() method (for Multiple files)
10
Shared Preferences (Cont.)
getPreferences()
For single Shared preference file we need to initialize the SharedPreferences object by
using getPreferences() method like as shown below.
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
11
Shared Preferences (Cont.)
getSharedPreferences()
- This method is useful to get the values from multiple shared preference files
- Filename is passed as a parameter to access data from the specific file
For single Shared preference file we need to initialize the SharedPreferences object by
using getPreferences() method like as shown below.
SharedPreferences sharedPref =
getSharedPreferences("filename1",Context.MODE_PRIVATE);
12
Shared Preferences (Cont.)
To control access permission to the files we have following modes:
MODE_PRIVATE
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
13
Shared Preferences (Cont.)
Write to Shared Preferences (Store data)
To store data in a shared preference file, we need an editor to edit and save the changes
in the SharedPreferences object.
14
Shared Preferences (Cont.)
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
editor.putBoolean("keyname",true);
editor.putString("keyname","string value");
editor.putInt("keyname","int value");
editor.putFloat("keyname","float value");
editor.putLong("keyname","long value");
16
Shared Preferences (Cont.)
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
pref.getString("keyname",null);
pref.getInt("keyname",0);
pref.getFloat("keyname",0);
pref.getBoolean("keyname",true);
pref.getLong("keyname",0);
17
Shared Preferences (Cont.)
Deleting from Shared Preferences
To delete values from the Shared Preferences file, we need to call remove() method by
providing the key for the value which we want to delete
18
Shared Preferences (Cont.)
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
editor.remove("keyname");
editor.commit();
19
Shared Preferences (Cont.)
Clearing the Shared Preferences
We can clear all the data from Shared Preferences file using a clear() method
20
Shared Preferences (Cont.)
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
editor.clear();
editor.commit();
21
Shared Preferences Internal Structure
● Shared preferences internally has an in-memory storage on the top of disk storage
● Every operation goes through in-memory storage first & then to the disk.
● In-memory storage is basically a HashMap which allows O(1) time complexity for
all operations.
In-Memory Disk
HashMap XML
22
Shared Preferences (Cont.)
Save Data via Commit
● It saves data in to in-memory storage first & then synchronously writes it to the
disk.
● Since IO operation is involved, main thread is blocked until the data is written to
the disk.
● Due to write operation is synchronous, success status is returned as a boolean.
● Using the return value you can verify the operation is successful or not.
Val result = pref.edit().putString(key, “value”).commit()
23
Shared Preferences (Cont.)
Save Data via Commit
Val result = pref.edit().putString(key, “value”).commit()
HashMap XML
24
Shared Preferences (Cont.)
Save Data via apply
● It saves data in to in-memory storage first & then asynchronously writes it to the
disk.
● Since IO operation is involved, main thread is not blocked.
● Due to write operation is asynchronous, success status is not returned .
● So you cannot verify the operation is successful or not.
pref.edit().putString(key, “value”).apply()
25
Shared Preferences (Cont.)
Save Data via apply
pref.edit().putString(key, “value”).apply()
Worker Thread
26
Shared Preferences (Cont.)
Get the data
27
Shared Preferences (Cont.)
Get the data
Val result = pref.getString(key, null)
pref.getString(key, null)
In-Memory Disk
HashMap XML
28
Shared Preferences (Cont.)
Some use case of Shared Preferences
29
Shared Preferences (Cont.)
Our Example:
Save load
next back
Shared
Preferences
Main Activity Activity B
30
Shared Preferences Example (Code)
Activity_main.xml file
AndroidManifest.xml file
MainActivity.java file
validateActivity.java file
Validate.xml file
31