Classes For A Given Application
Classes For A Given Application
What Is A Preference?
Shared preferences are simply sets of data values that are stored persistently. By persistence, we
are talking about data that persists across application lifecycle events. In other words, the application
(or device, for that matter) can be started and stopped without losing the data. The next time the
user launches the application, that data will still be available.
An individual preference is simply a key-value pair with a specific data type for the value. The
preference key is simply a string that uniquely identifies the preference and the value is just that: the
value of that preference.
For example, your application might want to store the users name. The application could have a
single preference to store this information:
A preference can be any of a number of different data types. The following data types are supported
by the SharedPreferences class:
Boolean values
Float values
Integer values
Long values
String values
1.
2.
import android.content.SharedPreferences;
3.
4.
SharedPreferences settings =
getSharedPreferences("MyGamePreferences", MODE_PRIVATE);
There is no limit to the number of sets of shared preferences your application can have. How your
application organizes its preferences is up to you. However, you may want to declare your
preference set names so that you can easily load and access the preferences from any Activity
within your application. For example:
view plaincopy to clipboardprint?
1.
An activity can also have private preferences. These preferences are only available within the
specific Activity class and are not shared with other activities. An activity can only have one set of
private preferences. The following code retrieves the activitys private preferences:
view plaincopy to clipboardprint?
1.
2.
3.
import android.content.SharedPreferences;
Setting Preferences
Saving preferences to your application is fairly straightforward. First, you must decide if you want
application or activity preferences. Use the appropriate method to retrieve the appropriate
SharedPreferences object: use the getPreferences() method of the Activity class for activity-level
preferences or the getSharedPreferences() method of the Context class for application-level
preferences.
Once you have a valid SharedPreferences object, you must use a SharedPreferences.Editor to add,
modify, or delete preference content. To retrieve an Editor for a specific SharedPreferences object,
use its edit() method. Make any changes to the preferences using the methods available in the
Editor class. For example, the SharedPreferences.Editor class has helper methods for saving
preferences of different data types:
1.
import android.content.SharedPreferences;
2.
3.
4.
5.
6.
7.
Updating Preferences
Updating preferences is as simple as retrieving another SharedPreferences.Editor and making
changes to a given preference by name. For example, the following code modifies the PaidUser
preference:
1.
2.
3.
4.
Tip: As you can see, it can be useful to define each preference key as static final string variables so
that a given preference is always stored in a regular, reproducible fashion. You dont want random
preference strings floating around in your code.
Retrieving Preferences
You dont need an Editor to simply read preferences. Instead, retrieve the SharedPreferences object
and use the appropriate method to retrieve a preference by name:
Conclusion
Shared preferences (android.content.SharedPreferences) can be used to easily store application
data. Application preferences are stored as key-value pairs, and can be many different data types,
including numbers, strings and Boolean values. Different sets of preferences can be stored in named
preference sets. Use shared preferences to store simple application data in a persistent manner.