Persisting data with SharedPreferences
In Android, there are a few ways to make data persist. By persist, I mean that if the user quits the app, then when they come back to it, their data is still available. Which method is the correct one to use is dependent upon the app and the type of data.
In this book, we will look at three ways to make data persist. For saving our users' settings, we only need a simple method. After all, we just need to know whether they want the decorative divider between each of the notes in the RecyclerView
widget.
Let's look at how we can make our apps save and reload variables to the internal storage of the device. We need to use the SharedPreferences
class. SharedPreferences
is a class that provides access to data that can be accessed and edited by all Activity
classes of an app. Let's look at how we can use it:
// A SharedPreferences for reading data SharedPreferences prefs; // A SharedPreferences.Editor for writing data SharedPreferences...