Persisting data with SharedPreferences
In Android, there are a number of ways to make data persist. By persist, I mean that if the user quits the app, when they come back to it, their data is still available. The correct method to use is dependent upon the app and 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 really simple method. After all, we just need to know if they want sound and at which speed they want their animations.
Let's take a 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 activities 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.Editor editor;
As with all objects, we need...