Data Persistence in Android – SharedPreferences, SQLite, and Room Database
Learn how to store data persistently in Android using SharedPreferences, SQLite, and Room Database. Understand the use cases for each method and how to implement them effectively.
Data Persistence in Android
Data persistence is crucial for Android applications that need to save user preferences, app settings, or even larger amounts of data locally. In this tutorial, we will explore three main ways to persist data in Android:
- SharedPreferences – For saving small key-value pairs such as user settings.
- SQLite – For working with structured data in a relational format.
- Room Database – A modern, abstraction layer over SQLite that simplifies database operations.
i) Using SharedPreferences for Small Data Storage
SharedPreferences is a simple key-value pair storage system in Android that is mainly used for storing small amounts of data, such as user preferences or app settings. It allows data to be saved as key-value pairs and is persisted across app restarts.
How to Use SharedPreferences
- Saving Data:
To save data in SharedPreferences, you use an instance of the SharedPreferences.Editor class to put data (such as strings, booleans, etc.) and then commit or apply the changes.
- Retrieving Data:
To retrieve data, simply call the get methods from SharedPreferences, specifying the key and default value if the key does not exist.
- Removing Data:
You can remove specific key-value pairs using remove() or clear all stored data with clear().
ii) Working with SQLite for Local Databases
SQLite is a powerful relational database that comes pre-installed in Android devices. It is ideal for storing larger datasets in a structured format using tables, rows, and columns. Although SQLite provides flexibility, developers often need to work with raw SQL queries, which can be complex.
How to Use SQLite
- Creating a Database:
To use SQLite, you'll typically extend the SQLiteOpenHelper class. This class helps you manage database creation, versioning, and upgrades.
- Inserting Data:
To insert data into the database, you'll use the insert() method of the SQLiteDatabase class.
- Querying Data:
To retrieve data, you can use query() or rawQuery(). Here's an example of using rawQuery() to fetch data:
- Updating and Deleting Data:
You can update or delete records using update() and delete() methods.
iii) Using Room Database as a Modern Abstraction Layer over SQLite
Room Database is a modern, higher-level abstraction over SQLite that makes database operations easier by providing a clean API and avoiding the need for writing raw SQL queries. It integrates with the Lifecycle library, making it easier to work with persistent data in a more efficient and readable way.
How to Use Room Database
- Setting Up Room:
First, add dependencies to your build.gradle file:
- Create an Entity:
In Room, an Entity is a class that represents a table in the database. Each field in the class corresponds to a column in the table.
- Create a DAO (Data Access Object):
A DAO is an interface that defines methods for accessing the database. It abstracts the database operations, allowing you to perform CRUD operations.
- Create the Database:
The database class extends RoomDatabase and provides an instance of the database.
- Using Room Database:
To use Room, you get an instance of the database and call DAO methods to interact with the database.
Conclusion
In this tutorial, you’ve learned how to persist data in Android using SharedPreferences, SQLite, and Room Database. SharedPreferences is great for small key-value pairs, SQLite provides a relational database for complex data, and Room Database simplifies SQLite interactions with a higher-level API.
By understanding and implementing these storage solutions, you can make your Android apps more efficient and capable of storing data across app launches.