How do you save/store objects in SharedPreferences on Android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
SharedPreferences in Android
SharedPreferences is one of the most fundamental approaches to persist lightweight data in Android. It is primarily used for saving primitive data types (like boolean, float, int, long, and strings) and is best suited for trivial key-value pairs, such as user preferences or settings.
Understanding SharedPreferences
SharedPreferences provides a simple way to persist data by storing key-value pairs. Internally, it uses a file storage mechanism where data is stored as XML files, making it both lightweight and relatively efficient. SharedPreferences is ideal for situations where you need to retain small amounts of data between application executions, such as user settings, login states, or application-level flags.
Key Characteristics of SharedPreferences:
- Storage of Simple Data: Best suited for small datasets like settings or flags.
- Private or Public Modes: Data can be stored in private or public mode.
- Synchronous vs. Asynchronous Storage: Supports both synchronous writes using `commit()` and asynchronous writes with `apply()`.
- Singleton Access: Ensures single instance access and retrieval of saved data.
Working with SharedPreferences
To efficiently leverage SharedPreferences, you'll need to follow a few systematic steps:
- Create or Retrieve a SharedPreferences Object: Access it using the `getSharedPreferences()` method of `Context`. You can open or create a preferences file, depending on its existence, as follows:
- Choosing between `apply()` and `commit()`:
- `apply()`: Asynchronous and does not return any boolean result. It's preferable for non-blocking operations.
- `commit()`: Synchronous and returns a boolean value, providing success or failure feedback. It's generally avoided in main threads due to potential UI blockage.
- Limitations:
- SharedPreferences are not suitable for storing complex data structures or large datasets. Consider using a database or a file for more sophisticated data needs.
- Safety and Security:
- Prefer using `MODE_PRIVATE` to ensure that your preferences are created with the appropriate level of privacy.
- Avoid storing sensitive data like passwords or encryption keys in SharedPreferences, opting for more secure solutions like Android's KeyStore.
Related reading
- How do you specify a byte literal in Java?
- How do you specify the Java compiler version in a pom.xml file?
- How do you use both Spring Data JPA and Spring Data Elasticsearch repositories on the same domain class in a Spring Boot application?
- How do you use the MySQL replication driver in Grails on Tomcat?
- How do you set EditText to only accept numeric values in Android?
- How do you share data between view controllers and other objects in Swift?
- How does a ArrayList's contains() method evaluate objects?
- How does a Spring Boot console based application work?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.