Using SharedPreferences and DataStore
Imagine you are tasked with integrating a third party that uses something such as OAuth to implement logging in with Facebook, Google, and so on. These mechanisms work as follows: they give you a token to store locally, which can be used to send other requests to access user data.
This raises several questions. How can you store that token? Do you use Room
just for one token? Do you save the token in a separate file and implement methods for writing the file? What if that file must be accessed in multiple places at the same time? SharedPreferences
and DataStore
are the answers to these questions. SharedPreferences
is a functionality that allows you to save Booleans, integers, floats, longs, strings, and sets of strings into an XML file.
When you want to save new values, you specify what values you want to save for the associated keys, and when you are done, you commit the change, which will trigger the save to the XML file in an asynchronous...