Preferences and DataStore
Imagine you are tasked with integrating a third-party API that uses something such as OAuth to implement logging in with Facebook, Google, and suchlike. The way these mechanisms work is as follows – they give you a token that you have to store locally and that can then 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 has to be accessed in multiple places at the same time? SharedPreferences
and DataStore
are 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...