Creating a Preference Activity
To display your settings in an activity, extend the PreferenceActivity
class. This is an extension of the traditional Activity
class that displays a list of settings based on a hierarchy of Preference
objects. ThePreferenceActivity
automatically persists the settings associated with each Preference
when the user makes a change.
Note: If you're developing your application for Android 3.0 and higher, you should instead usePreferenceFragment
. Go to the next section about Using Preference Fragments.
The most important thing to remember is that you do not load a layout of views during the onCreate()
callback. Instead, you call addPreferencesFromResource()
to add the preferences you've declared in an XML file to the activity. For example, here's the bare minimum code required for a functional PreferenceActivity
:
public class SettingsActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
This is actually enough code for some apps, because as soon as the user modifies a preference, the system saves the changes to a default SharedPreferences
file that your other application components can read when you need to check the user's settings. Many apps, however, require a little more code in order to listen for changes that occur to the preferences. For information about listening to changes in the SharedPreferences
file, see the section about Reading Preferences.
Using Preference Fragments
If you're developing for Android 3.0 (API level 11) and higher, you should use a PreferenceFragment
to display your list of Preference
objects. You can add a PreferenceFragment
to any activity—you don't need to usePreferenceActivity
.
Fragments provide a more flexible architecture for your application, compared to using activities alone, no matter what kind of activity you're building. As such, we suggest you use PreferenceFragment
to control the display of your settings instead of PreferenceActivity
when possible.
Your implementation of PreferenceFragment
can be as simple as defining the onCreate()
method to load a preferences file with addPreferencesFromResource()
. For example:
public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); } ... }
You can then add this fragment to an Activity
just as you would for any other Fragment
. For example:
public class SettingsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display the fragment as the main content. getFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); } }
Note: A PreferenceFragment
doesn't have a its own Context
object. If you need a Context
object, you can call getActivity()
. However, be careful to call getActivity()
only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle,getActivity()
will return null.