Member-only story
Meet DataStore — The New SharedPreferences for Android
Learn about the basic concepts of DataStore in Android Kotlin and why you should stop using SharedPreferences in Android and utilize the Google’s new DataStore API
Written by Wajahat Karim and Muhammad Ali
To store some little amount of data in Android like phone number, name, or a person, etc. Android developers use SharedPreferences
API. But due to security and asynchronous transactions purpose, Google recommends using DataStore
instead. In this article, we will discuss the disadvantages of SharedPreferences
, and some basics of the new DataStore
API with its usage.
A Bit About SharedPreferences
The SharedPreferences
APIs are for reading and writing key-value pairs stored in an XML based file in the data
directory of the Android device. The SharedPreferences
allows app developers to store small amount of data like String, Int, Long, Double etc. for things like configs, settings etc. You can still put complex custom class objects through Serializable
, but that's not usually recommended.
To use it, first you need to get an instance of SharedPreferences
and then you can either read the data with the get*()
methods like example…