Simple Preferences & Proto DataStore Demo App

Beginner’s friendly step-by-step guide to learn how to use Preferences and Proto DataStore, Room Database is not covered.

Vincent Tsen

--

There are a few ways you can store data locally on your Android devices:

  • SharedPreferences (replaced by Preferences DataStore)
  • Preferences DataStore
  • Proto DataStore
  • Room Database

SharedPreferences is the old way, which has been replaced by Preferences DataStore due to its shortcomings (e.g. not safe to be called from the UI thread).

In this article, I’m going to share how to store data using both Preferences DataStore and Proto DataStore. Room Database will be covered later in the next article.

Prefer which way to store data?

Preferences DataStore stores data in a simple key-value format. It is usually for a small and simple dataset. If you have a more complex data format, use Proto DataStore.

Proto DataStore stores data in custom data type format, which is defined using protocol buffers. This is usually for a medium size dataset and it doesn’t support partial updates. So for a very large dataset, it is better to use the Room Database.

Room Database is similar to Proto DataStore which can store data in custom data type in a relational database (SQLite) format. Since it supports partial updates, so there won’t be any problem storing a very large and complex Database.

Preferences DataStore

Here is a simple app example to store 2 setting values in preferences DataStore. One is Boolean format and another is Int format.

1. Add Preferences DataStore Library

dependencies {…

--

--