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 17-1.

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

Figure 17-1: The preference screen for the call settings in Android.

image

A PreferenceActivity is a base class that is very 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 display of preferences in the visual style that resembles the system preferences. This gives your application a feel consistent with other Android user interface components. You use the PreferenceActivity when dealing with preference screens in your Android applications.

Persisting preference values

Because the Android framework stores preferences in the SharedPreferences, which automatically stores the preference data in internal storage, creating a preference is easy. When a user edits a preference, the value is automatically saved for you — that's right, you don't have to do any persisting yourself!

I'm sure this sounds like a little bit of black magic, but I assure ...

Get Android™ Tablet Application Development For Dummies® 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.