Stating Your Preference

Given the appropriate SharedPreferences object, you can use edit() to get an "editor" for the preferences. This object has a group of setters that mirror the getters on the parent SharedPreferences object. It also has the following:

• remove() to get rid of a single named preference

• clear() to get rid of all preferences

• commit() to persist your changes made via the editor

The last one is important—if you modify preferences via the editor and fail to commit() the changes, those changes will evaporate once the editor goes out of scope.

Conversely, since the preferences object supports live changes, if one part of your application (say, an activity) modifies shared preferences, another part of your application (say, a service) will have access to the changed value immediately.

0 0

Post a comment