Android
SharedPreferences
Object Storage
Android Development
Java

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.

Browse interview questions

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:

  1. Storage of Simple Data: Best suited for small datasets like settings or flags.
  2. Private or Public Modes: Data can be stored in private or public mode.
  3. Synchronous vs. Asynchronous Storage: Supports both synchronous writes using `commit()` and asynchronous writes with `apply()`.
  4. 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:

  1. 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.