Shared Preferences is an essential feature in mobile app development that lets developers store and retrieve small amounts of data as key-value pairs. The mechanism is especially useful for storing user settings, app states or other configuration data that must be kept between app sessions. Shared Preferences is part of the Android platform and offers a simple and efficient way to handle persistent data without the need for complex database setups.
When an app uses Shared Preferences, the data is stored in an XML file in the app’s internal storage. This lets the app access and update the data quickly, which improves the user experience by keeping the user’s preferences and settings available and up to date. Shared Preferences is designed to be lightweight and private to the app, but it is not encrypted, which makes it an ideal choice for non-sensitive data in apps optimised for both performance and usability.
Shared Preferences is a simple and efficient way of storing small amounts of data as key-value pairs in the device's local storage, allowing mobile applications to keep user settings and application states between sessions. This functionality is ideal for storing user preferences, such as theme settings or whether a user is logged in, as it offers a lightweight solution without the need for a database. The data remains available to the application even after it has been closed or restarted, which ensures a consistent and personal user experience. Shared Preferences is especially useful where fast access to persistent data is needed and the security requirements are moderate, as the data is not encrypted by default; passwords and other secrets should not be stored in it unencrypted.
More about Shared Preferences
Shared Preferences is an essential technology in mobile app development that enables simple and efficient storage of small amounts of data as key-value pairs. The method is ideal for storing user settings, application preferences and other persistent data that must be kept between app sessions. Shared Preferences is particularly well suited to cases where fast access to data and ease of use are crucial, such as user settings, login status and app configuration.
Technically, Shared Preferences stores data in an XML file located in the app’s internal storage. When an app wants to store or retrieve data, it interacts with this file through an API that handles read and write operations. This makes Shared Preferences both efficient and easy to implement, which is a big advantage for developers who want lightweight data management without having to handle complex database operations.
Shared Preferences can be used in different scenarios, each supporting a different type of data handling:
Storing user settings: Shared Preferences is used to store user-defined settings such as theme, notification preferences or language choice. This ensures that the user’s preferences are kept even when the app is closed and reopened.
Session management: Shared Preferences can be used to store session-related data, such as login status or a user ID, which lets the app remember the user between sessions without repeated logins. Access tokens and other secrets should be encrypted before they are stored.
Configuration data: Applications can store configuration data needed for the app’s functionality, such as feature settings or default values, which makes it easy to adjust the app’s behaviour without changing the code. Secret API keys do not belong in Shared Preferences, as anything stored on the device can be extracted.
One of the greatest strengths of Shared Preferences is its simplicity and efficiency. Easy access to data via key-value pairs lets developers implement persistent data storage quickly without having to worry about complex database operations. The data is also only accessible to the specific app, which provides a certain degree of security and privacy.
The low resource consumption of Shared Preferences is another important advantage. The technology requires minimal storage space and processing power, which makes it ideal for mobile devices where resource optimisation is critical. Since the data is stored in the app’s internal storage, it is also normally protected against access from other apps.
In practice, Shared Preferences has long been the standard way of storing simple user data in Android apps. It is used to keep the user experience consistent and personal by remembering the user’s preferences and status. It is also used to optimise the app’s performance by reducing the need for repeated data requests and network calls.
In summary, Shared Preferences is a technology that combines simplicity and efficiency in a solution that makes data storage more intuitive and accessible for developers. Its ability to work without complex setup and with minimal resource consumption makes it an important tool in modern app development, where fast and reliable data handling is essential.
Frequently asked questions about Shared Preferences
What are Shared Preferences, and how do they work in Android development?
Shared Preferences in Android development is a mechanism for storing simple data as key-value pairs that can be accessed across different components of an application. The method is ideal for storing small amounts of data, such as user settings or application configuration, that do not require a database.
When an application uses Shared Preferences, a file is created containing the stored data, and this file is only accessible to the application that created it. The data is stored in an XML file in the application’s internal storage, which protects it from access by other apps on a normal, non-rooted device. Developers can easily read from and write to this file using methods such as getSharedPreferences(), edit(), putString() and commit() or apply() to save the changes.
Compared with more complex storage solutions such as SQLite databases, Shared Preferences is far simpler and quicker to implement, especially when only simple data needs to be stored that does not require structured storage or complex queries. This makes Shared Preferences an efficient choice for many applications that need to store and retrieve small amounts of data quickly and easily.
How is data stored and retrieved using Shared Preferences?
Data is stored in Shared Preferences as key-value pairs. First, a SharedPreferences object is obtained, typically with getSharedPreferences and a file name. To store data, an editor is opened with edit(), values are added with methods such as putString or putBoolean, and the changes are saved with apply(), which writes asynchronously, or commit(), which writes immediately.
Data is retrieved with methods such as getString or getInt, specifying the key and a default value that is used if the key does not exist. For newer Android projects, Google often recommends DataStore as a more modern replacement, but the principle of keys and values is the same.
Which data types can be stored in Shared Preferences?
Shared Preferences supports simple data types: text strings (String), integers (int), long integers (long), decimal numbers (float) and true/false values (boolean). A set of text strings (Set of String) can also be stored.
More complex objects cannot be stored directly. They can be converted to text, for example as JSON, but this is only recommended for small amounts of data. Shared Preferences is intended for lightweight information such as user settings, a chosen theme or whether an introduction has been shown. Larger or structured data belongs in a database such as Room or in files.
What is the difference between Shared Preferences and other storage methods such as SQLite?
Shared Preferences stores small amounts of data as key-value pairs in a file on the device. It is simple to use but is not suited to large or complex amounts of data, and proper queries are not possible.
SQLite, often via Room, is a relational database that can handle large amounts of structured data, relationships between tables and advanced searches. Files are well suited to images, documents and other larger data. DataStore is Google’s newer alternative to Shared Preferences with asynchronous access. The choice depends on the amount of data: settings in Shared Preferences or DataStore, structured data in a database.
How is data stored in Shared Preferences secured?
Data in Shared Preferences is stored as plain text in the app’s private folder. Other apps normally have no access, but on a rooted device or via certain backups the file can be read. Passwords, tokens and other sensitive information should therefore not be stored unencrypted.
Sensitive data can be encrypted before it is stored, using keys held in the Android Keystore, which is designed to protect cryptographic keys. The Jetpack library androidx.security:security-crypto previously offered EncryptedSharedPreferences for this purpose, but Google has marked the entire library, including EncryptedSharedPreferences, EncryptedFile and MasterKey, as deprecated from version 1.1.0-alpha07 in April 2025. Google’s recommendation is instead to use the platform’s regular APIs, such as SharedPreferences, together with direct use of the Android Keystore, where a key is generated with KeyGenerator and used to encrypt the values before they are stored. In general, as little sensitive information as possible should be stored on the device.
Which limitations should be kept in mind when using Shared Preferences?
Shared Preferences is intended for small amounts of simple data. Large amounts of data can slow the app down, because the whole file is loaded into memory, and writing with commit() on the main thread can make the user interface stutter.
There is no built-in support for complex objects, relationships or queries, and data is not encrypted automatically. There is also no type safety, so a wrong key or data type is only discovered when the app runs. Changes made from several processes at the same time are not handled reliably. For more advanced needs, DataStore, Room or encrypted storage is recommended.
Shared Preferences for dummies
Shared Preferences is a clever feature in mobile apps that lets the app remember small pieces of information it has been given. Think of it as a little notebook in which the app can jot down things it needs to remember for later. For example, an app that remembers a username, so it does not have to be typed in every time the app is opened, uses Shared Preferences to store that information. It is an easy and quick way for apps to store data that does not take up much space and needs to be available the next time the app is used.
