Introducing the Preference Activity in Android

The PreferenceActivity class is used to host the Preference Fragment hierarchy defined by a preference headers resource. Prior to Android 3.0, the Preference Activity was used to host Preference Screens directly. For applications that target devices prior to Android 3.0, you may still need to use the Preference Activity in this way

To create a new Preference Activity, extend the PreferenceActivity class as follows:

public class MyFragmentPreferenceActivity extends PreferenceActivity

When using Preference Fragments and headers, override the onBuildHeaders handler, calling loadHeadersFromResource and specifying your preference headers resource file:

public void onBuildHeaders(List<Header> target) {
 loadHeadersFromResource(R.xml.userpreferenceheaders, target);

For legacy applications, you can inflate the Preference Screen directly in the same way as you would from a Preference Fragment — by overriding the onCreate handler and calling add PreferencesFromResource, specifying the Preference Screen layout XML resource to display within that Activity:

public void onCreate(Bundle savedInstanceState) {

Like all Activities, the Preference Activity must be included in the application manifest:

<activity android:name=”.MyPreferenceActivity”
 android:label=”My Preferences”>

To display the application settings hosted in this Activity, open it by calling startActivity or startActivityForResult:

Intent i = new Intent(this, MyPreferenceActivity.class);
startActivityForResult(i, SHOW_PREFERENCES);

Leave a Comment