Understanding the PreferenceActivity Class

The responsibility of the PreferenceActivity class is to show a hierarchy of Preference objects as lists, possibly spanning multiple screens, as shown in Figure 15-1.

Figure 15-1: The preferences screen for the call settings in Android.

9781118417454-fg1501.eps

When preferences are edited, they’re stored using an instance of Shared Preferences. The SharedPreferences class is an interface for accessing and modifying preference data returned by getSharedPreferences() from any Context object.

A PreferenceActivity is a base class that’s similar to the Activity base class. However, the PreferenceActivity behaves a bit differently. One of the most important features that the PreferenceActivity handles is the displaying of preferences in the visual style that resembles the system preferences. This gives your application a consistent feel across the board in regard to Android user interface components. You should use the PreferenceActivity when dealing with preferences screens in your Android applications.

Persisting preference values

Because the Android framework stores preferences in the Shared Preferences, which automatically stores the preference data in internal storage, you can easily create a preference. When a user edits a preference, the value is automatically saved for you; you don’t have to do any persisting yourself.

Figure 15-2 shows a preference being ...

Get Android Application Development For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.