PreferenceFragmentCompat
(SESL variant) A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device. To retrieve an instance of android.content.SharedPreferences that the preference hierarchy in this fragment will use by default, call getDefaultSharedPreferences with a context in the same package as this fragment.
You can define a preference hierarchy as an XML resource, or you can build a hierarchy in code. In both cases you need to use a PreferenceScreen as the root component in your hierarchy.
To inflate from XML, use the setPreferencesFromResource. An example example XML resource is shown further down.
To build a hierarchy from code, use createPreferenceScreen to create the root PreferenceScreen. Once you have added other Preferences to this root screen with addPreference, you then need to set the screen as the root screen in your hierarchy with setPreferenceScreen.
As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick.
Sample Code
The following sample code shows a simple settings screen using an XML resource. The XML resource is as follows:
{@sample samples/SupportPreferenceDemos/src/main/res/xml/preferences.xml preferences}The fragment that loads the XML resource is as follows:
{@sample samples/SupportPreferenceDemos/src/main/java/com/example/androidx/preference * /Preferences.java preferences}See also
Types
Properties
Functions
key
.