One of the new classes that Honeycomb introduced was the PreferenceFragment. This class is meant to simplify the creatation of a setting / preferences screen in Android applications. It handles a lot of the displaying, saving, and changing of an application’s settings. There are a couple of ways to create a PreferenceFragment. The simplest way is to subclass, override onCreate() and then use either getPreferencesFromResource or getPreferencesFromIntent.
There are many examples on how to use getPreferencesFromResource, but I noticed that there aren’t that many on how to use getPreferencesFromIntent. Here is one such quick example.
The first thing to do is to create your PreferenceFragment and provide it with an Intent that identifies an Activity. This Activity will have some meta-data associated with it that the PreferenceFragment will use to create it’s layout (more on this later)
The next step is to create the Activity MyActivityWithPreferences (if you don’t already have it). The code inside the Activity doesn’t matter so much - what is important is how the Activity is declared in AndroidManifest.xml. I provide AndroidManifest.xml here:
Notice on line 16 the meta-data element that was added as a child of the activity element. PreferenceFragment will use the resource file res/xml/preference_from_intent.xml and inflate a preference heirarchy using that XML file. Here is an example one such XML file: