belongs to Maven artifact com.android.support:preference-v7:28.0.0-alpha1
PreferenceFragmentCompat
  public
  
  
  abstract
  class
  PreferenceFragmentCompat
  
  
  
  
    extends Fragment
  
  
  
  
  
      implements
      
        PreferenceManager.OnPreferenceTreeClickListener, 
      
        PreferenceManager.OnDisplayPreferenceDialogListener, 
      
        PreferenceManager.OnNavigateToScreenListener, 
      
        DialogPreference.TargetFragment
      
  
  
| java.lang.Object | ||
| ↳ | android.support.v4.app.Fragment | |
| ↳ | android.support.v7.preference.PreferenceFragmentCompat | |
Shows a hierarchy of Preference objects as
 lists. These preferences will
 automatically save to SharedPreferences as the user interacts with
 them. To retrieve an instance of SharedPreferences that the
 preference hierarchy in this fragment will use, call
 getDefaultSharedPreferences(android.content.Context)
 with a context in the same package as this fragment.
 
Furthermore, the preferences shown will follow the visual style of system preferences. It is easy to create a hierarchy of preferences (that can be shown on multiple screens) via XML. For these reasons, it is recommended to use this fragment (as a superclass) to deal with preferences in applications.
 A PreferenceScreen object should be at the top of the preference
 hierarchy. Furthermore, subsequent PreferenceScreen in the hierarchy
 denote a screen break--that is the preferences contained within subsequent
 PreferenceScreen should be shown on another screen. The preference
 framework handles this by calling onNavigateToScreen(PreferenceScreen).
 
The preference hierarchy can be formed in multiple ways:
Activities that each specify its own
 preferences in an XML file via Activity meta-data
 PreferenceScreen
 
 To inflate from XML, use the addPreferencesFromResource(int). The
 root element should be a PreferenceScreen. Subsequent elements can point
 to actual Preference subclasses. As mentioned above, subsequent
 PreferenceScreen in the hierarchy will result in the screen break.
 
 To specify an object hierarchy rooted with PreferenceScreen, use
 setPreferenceScreen(PreferenceScreen).
 
 As a convenience, this fragment implements a click listener for any
 preference in the current hierarchy, see
 onPreferenceTreeClick(Preference).
 
Developer Guides
For information about using PreferenceFragment,
 read the Settings
 guide.
Sample Code
The following sample code shows a simple preference fragment that is populated from a resource. The resource it loads is:
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/root_title">
    <Preference
        android:key="basic_preference"
        android:title="@string/title_basic_preference"
        android:summary="@string/summary_basic_preference" />
    <Preference
        android:key="stylish_preference"
        android:title="@string/title_stylish_preference"
        android:summary="@string/summary_stylish_preference" />
    <Preference
        android:key="preference_with_icon"
        android:title="Preference with icon"
        android:summary="This preference has an icon"
        android:icon="@android:drawable/ic_menu_camera" />
    <PreferenceCategory
        android:title="@string/inline_preferences">
        <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="@string/title_checkbox_preference"
            android:summary="@string/summary_checkbox_preference" />
        <SwitchPreference
            android:key="switch_preference"
            android:title="Switch preference"
            android:summary="This is a switch" />
        <DropDownPreference
            android:key="dropdown_preference"
            android:title="@string/title_dropdown_preference"
            android:summary="@string/summary_dropdown_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/dialog_based_preferences">
        <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />
        <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />
        <MultiSelectListPreference
            android:key="multi_select_list_preference"
            android:title="@string/title_multi_list_preference"
            android:summary="@string/summary_multi_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_multi_list_preference" />
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/launch_preferences">
        <!-- This PreferenceScreen tag serves as a screen break (similar to page break
             in word processing). Like for other preference types, we assign a key
             here so it is able to save and restore its instance state. -->
        <PreferenceScreen
            android:key="screen_preference"
            android:title="@string/title_screen_preference"
            android:summary="@string/summary_screen_preference">
            <!-- You can place more preferences here that will be shown on the next screen. -->
            <CheckBoxPreference
                android:key="next_screen_checkbox_preference"
                android:title="@string/title_next_screen_toggle_preference"
                android:summary="@string/summary_next_screen_toggle_preference" />
        </PreferenceScreen>
        <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">
            <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />
        </PreferenceScreen>
    </PreferenceCategory>
    <PreferenceCategory
        android:title="@string/preference_attributes">
        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/summary_parent_preference" />
        <!-- The visual style of a child is defined by this styled theme attribute. -->
        <CheckBoxPreference
            android:key="child_checkbox_preference"
            android:dependency="parent_checkbox_preference"
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />
    </PreferenceCategory>
</PreferenceScreen>The fragment implementation itself simply populates the preferences when created. Note that the preferences framework takes care of loading the current values out of the app preferences and writing them when changed:
public static class PrefsFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}See also:
Summary
| Nested classes | |
|---|---|
| 
        
        
        
        
        interface | PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback
 | 
| 
        
        
        
        
        interface | PreferenceFragmentCompat.OnPreferenceStartFragmentCallbackInterface that PreferenceFragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. | 
| 
        
        
        
        
        interface | PreferenceFragmentCompat.OnPreferenceStartScreenCallbackInterface that PreferenceFragment's containing activity should implement to be able to process preference items that wish to switch to a new screen of preferences. | 
| XML attributes | |
|---|---|
| PreferenceFragmentCompat_android_divider | |
| PreferenceFragmentCompat_android_dividerHeight | |
| Constants | |
|---|---|
| String | ARG_PREFERENCE_ROOTFragment argument used to specify the tag of the desired root
  | 
| Public constructors | |
|---|---|
| 
      PreferenceFragmentCompat()
       | |
| Public methods | |
|---|---|
| 
        
        
        
        
        
        void | 
      addPreferencesFromResource(int preferencesResId)
      Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy. | 
| 
        
        
        
        
        
        Preference | 
      findPreference(CharSequence key)
      Finds a  | 
| 
        
        
        
        final
        
        RecyclerView | 
      getListView()
       | 
| 
        
        
        
        
        
        PreferenceManager | 
      getPreferenceManager()
      Returns the  | 
| 
        
        
        
        
        
        PreferenceScreen | 
      getPreferenceScreen()
      Gets the root of the preference hierarchy that this fragment is showing. | 
| 
        
        
        
        
        
        void | 
      onActivityCreated(Bundle savedInstanceState)
      Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. | 
| 
        
        
        
        
        
        void | 
      onCreate(Bundle savedInstanceState)
      Called to do initial creation of a fragment. | 
| 
        
        
        
        
        
        RecyclerView.LayoutManager | 
      onCreateLayoutManager()
      Called from  | 
| 
        abstract
        
        
        
        
        void | 
      onCreatePreferences(Bundle savedInstanceState, String rootKey)
      Called during  | 
| 
        
        
        
        
        
        RecyclerView | 
      onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
      Creates the  | 
| 
        
        
        
        
        
        View | 
      onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      Called to have the fragment instantiate its user interface view. | 
| 
        
        
        
        
        
        void | 
      onDestroyView()
      Called when the view previously created by  | 
| 
        
        
        
        
        
        void | 
      onDisplayPreferenceDialog(Preference preference)
      Called when a preference in the tree requests to display a dialog. | 
| 
        
        
        
        
        
        void | 
      onNavigateToScreen(PreferenceScreen preferenceScreen)
      Called by
  | 
| 
        
        
        
        
        
        boolean | 
      onPreferenceTreeClick(Preference preference)
      Called when a preference in the tree rooted at this
  | 
| 
        
        
        
        
        
        void | 
      onSaveInstanceState(Bundle outState)
      Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance of its process is restarted. | 
| 
        
        
        
        
        
        void | 
      onStart()
      Called when the Fragment is visible to the user. | 
| 
        
        
        
        
        
        void | 
      onStop()
      Called when the Fragment is no longer started. | 
| 
        
        
        
        
        
        void | 
      onViewCreated(View view, Bundle savedInstanceState)
      Called immediately after  | 
| 
        
        
        
        
        
        void | 
      scrollToPreference(Preference preference)
       | 
| 
        
        
        
        
        
        void | 
      scrollToPreference(String key)
       | 
| 
        
        
        
        
        
        void | 
      setDivider(Drawable divider)
      Sets the drawable that will be drawn between each item in the list. | 
| 
        
        
        
        
        
        void | 
      setDividerHeight(int height)
      Sets the height of the divider that will be drawn between each item in the list. | 
| 
        
        
        
        
        
        void | 
      setPreferenceScreen(PreferenceScreen preferenceScreen)
      Sets the root of the preference hierarchy that this fragment is showing. | 
| 
        
        
        
        
        
        void | 
      setPreferencesFromResource(int preferencesResId, String key)
      Inflates the given XML resource and replaces the current preference hierarchy (if any) with
 the preference hierarchy rooted at  | 
| Protected methods | |
|---|---|
| 
        
        
        
        
        
        Adapter | 
      onCreateAdapter(PreferenceScreen preferenceScreen)
      Creates the root adapter. | 
| Inherited methods | |
|---|---|
|  From
class 
  
    android.support.v4.app.Fragment
  
 | |
|  From
class 
  
    java.lang.Object
  
 | |
|  From
interface 
  
    android.content.ComponentCallbacks
  
 | |
|  From
interface 
  
    android.view.View.OnCreateContextMenuListener
  
 | |
|  From
interface 
  
    android.arch.lifecycle.LifecycleOwner
  
 | |
|  From
interface 
  
    android.arch.lifecycle.ViewModelStoreOwner
  
 | |
|  From
interface 
  
    android.support.v7.preference.PreferenceManager.OnPreferenceTreeClickListener
  
 | |
|  From
interface 
  
    android.support.v7.preference.PreferenceManager.OnDisplayPreferenceDialogListener
  
 | |
|  From
interface 
  
    android.support.v7.preference.PreferenceManager.OnNavigateToScreenListener
  
 | |
|  From
interface 
  
    android.support.v7.preference.DialogPreference.TargetFragment
  
 | |
XML attributes
PreferenceFragmentCompat_android_divider
Related methods:
PreferenceFragmentCompat_android_dividerHeight
Related methods:
Constants
ARG_PREFERENCE_ROOT
String ARG_PREFERENCE_ROOT
Fragment argument used to specify the tag of the desired root
 PreferenceScreen object.
Constant Value: "android.support.v7.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"
Public constructors
Public methods
addPreferencesFromResource
void addPreferencesFromResource (int preferencesResId)
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
| Parameters | |
|---|---|
| preferencesResId | int: The XML resource ID to inflate. | 
findPreference
Preference findPreference (CharSequence key)
Finds a Preference based on its key.
| Parameters | |
|---|---|
| key | CharSequence: The key of the preference to retrieve. | 
| Returns | |
|---|---|
| Preference | The Preferencewith the key, or null. | 
See also:
getPreferenceManager
PreferenceManager getPreferenceManager ()
Returns the PreferenceManager used by this fragment.
| Returns | |
|---|---|
| PreferenceManager | The PreferenceManager. | 
getPreferenceScreen
PreferenceScreen getPreferenceScreen ()
Gets the root of the preference hierarchy that this fragment is showing.
| Returns | |
|---|---|
| PreferenceScreen | The PreferenceScreenthat is the root of the preference
         hierarchy. | 
onActivityCreated
void onActivityCreated (Bundle savedInstanceState)
Called when the fragment's activity has been created and this
 fragment's view hierarchy instantiated.  It can be used to do final
 initialization once these pieces are in place, such as retrieving
 views or restoring state.  It is also useful for fragments that use
 setRetainInstance(boolean) to retain their instance,
 as this callback tells the fragment when it is fully associated with
 the new activity instance.  This is called after onCreateView(LayoutInflater, ViewGroup, Bundle)
 and before onViewStateRestored(Bundle).
| Parameters | |
|---|---|
| savedInstanceState | Bundle: If the fragment is being re-created from
 a previous saved state, this is the state. | 
onCreate
void onCreate (Bundle savedInstanceState)
Called to do initial creation of a fragment.  This is called after
 onAttach(Activity) and before
 onCreateView(LayoutInflater, ViewGroup, Bundle).
 
Note that this can be called while the fragment's activity is
 still in the process of being created.  As such, you can not rely
 on things like the activity's content view hierarchy being initialized
 at this point.  If you want to do work once the activity itself is
 created, see onActivityCreated(Bundle).
 
Any restored child fragments will be created before the base
 Fragment.onCreate method returns.
| Parameters | |
|---|---|
| savedInstanceState | Bundle: If the fragment is being re-created from
 a previous saved state, this is the state. | 
onCreateLayoutManager
RecyclerView.LayoutManager onCreateLayoutManager ()
Called from onCreateRecyclerView(LayoutInflater, ViewGroup, Bundle) to create the
 RecyclerView.LayoutManager for the created
 RecyclerView.
| Returns | |
|---|---|
| RecyclerView.LayoutManager | A new RecyclerView.LayoutManagerinstance. | 
onCreatePreferences
void onCreatePreferences (Bundle savedInstanceState, String rootKey)
Called during onCreate(Bundle) to supply the preferences for this fragment.
 Subclasses are expected to call setPreferenceScreen(PreferenceScreen) either
 directly or via helper methods such as addPreferencesFromResource(int).
| Parameters | |
|---|---|
| savedInstanceState | Bundle: If the fragment is being re-created from
                           a previous saved state, this is the state. | 
| rootKey | String: If non-null, this preference fragment should be rooted at thePreferenceScreenwith this key. | 
onCreateRecyclerView
RecyclerView onCreateRecyclerView (LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
Creates the RecyclerView used to display the preferences.
 Subclasses may override this to return a customized
 RecyclerView.
| Parameters | |
|---|---|
| inflater | LayoutInflater: The LayoutInflater object that can be used to inflate theRecyclerView. | 
| parent | ViewGroup: The parentViewthat the RecyclerView will be attached to.
               This method should not add the view itself, but this can be used to generate
               the LayoutParams of the view. | 
| savedInstanceState | Bundle: If non-null, this view is being re-constructed from a previous
                           saved state as given here | 
| Returns | |
|---|---|
| RecyclerView | A new RecyclerView object to be placed into the view hierarchy | 
onCreateView
View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Called to have the fragment instantiate its user interface view.
 This is optional, and non-graphical fragments can return null (which
 is the default implementation).  This will be called between
 onCreate(Bundle) and onActivityCreated(Bundle).
 
If you return a View from here, you will later be called in
 onDestroyView() when the view is being released.
| Parameters | |
|---|---|
| inflater | LayoutInflater: The LayoutInflater object that can be used to inflate
 any views in the fragment, | 
| container | ViewGroup: If non-null, this is the parent view that the fragment's
 UI should be attached to.  The fragment should not add the view itself,
 but this can be used to generate the LayoutParams of the view. | 
| savedInstanceState | Bundle: If non-null, this fragment is being re-constructed
 from a previous saved state as given here. | 
| Returns | |
|---|---|
| View | Return the View for the fragment's UI, or null. | 
onDestroyView
void onDestroyView ()
Called when the view previously created by onCreateView(LayoutInflater, ViewGroup, Bundle) has
 been detached from the fragment.  The next time the fragment needs
 to be displayed, a new view will be created.  This is called
 after onStop() and before onDestroy().  It is called
 regardless of whether onCreateView(LayoutInflater, ViewGroup, Bundle) returned a
 non-null view.  Internally it is called after the view's state has
 been saved but before it has been removed from its parent.
onDisplayPreferenceDialog
void onDisplayPreferenceDialog (Preference preference)
Called when a preference in the tree requests to display a dialog. Subclasses should override this method to display custom dialogs or to handle dialogs for custom preference classes.
| Parameters | |
|---|---|
| preference | Preference: The Preference object requesting the dialog. | 
onNavigateToScreen
void onNavigateToScreen (PreferenceScreen preferenceScreen)
Called by
 onClick() in order to navigate to a
 new screen of preferences. Calls
 onPreferenceStartScreen(PreferenceFragmentCompat, PreferenceScreen)
 if the target fragment or containing activity implements
 PreferenceFragmentCompat.OnPreferenceStartScreenCallback.
| Parameters | |
|---|---|
| preferenceScreen | PreferenceScreen: ThePreferenceScreento
                         navigate to. | 
onPreferenceTreeClick
boolean onPreferenceTreeClick (Preference preference)
Called when a preference in the tree rooted at this
 PreferenceScreen has been clicked.
| Parameters | |
|---|---|
| preference | Preference: The preference that was clicked. | 
| Returns | |
|---|---|
| boolean | Whether the click was handled. | 
onSaveInstanceState
void onSaveInstanceState (Bundle outState)
Called to ask the fragment to save its current dynamic state, so it
 can later be reconstructed in a new instance of its process is
 restarted.  If a new instance of the fragment later needs to be
 created, the data you place in the Bundle here will be available
 in the Bundle given to onCreate(Bundle),
 onCreateView(LayoutInflater, ViewGroup, Bundle), and
 onActivityCreated(Bundle).
 
This corresponds to Activity.onSaveInstanceState(Bundle) and most of the discussion there
 applies here as well.  Note however: this method may be called
 at any time before onDestroy().  There are many situations
 where a fragment may be mostly torn down (such as when placed on the
 back stack with no UI showing), but its state will not be saved until
 its owning activity actually needs to save its state.
| Parameters | |
|---|---|
| outState | Bundle: Bundle in which to place your saved state. | 
onStart
void onStart ()
Called when the Fragment is visible to the user.  This is generally
 tied to Activity.onStart of the containing
 Activity's lifecycle.
onStop
void onStop ()
Called when the Fragment is no longer started.  This is generally
 tied to Activity.onStop of the containing
 Activity's lifecycle.
onViewCreated
void onViewCreated (View view, Bundle savedInstanceState)
Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle)
 has returned, but before any saved state has been restored in to the view.
 This gives subclasses a chance to initialize themselves once
 they know their view hierarchy has been completely created.  The fragment's
 view hierarchy is not however attached to its parent at this point.
| Parameters | |
|---|---|
| view | View: The View returned byonCreateView(LayoutInflater, ViewGroup, Bundle). | 
| savedInstanceState | Bundle: If non-null, this fragment is being re-constructed
 from a previous saved state as given here. | 
scrollToPreference
void scrollToPreference (Preference preference)
| Parameters | |
|---|---|
| preference | Preference | 
scrollToPreference
void scrollToPreference (String key)
| Parameters | |
|---|---|
| key | String | 
setDivider
void setDivider (Drawable divider)
Sets the drawable that will be drawn between each item in the list.
 Note: If the drawable does not have an intrinsic
 height, you should also call setDividerHeight(int).
Related XML Attributes:
| Parameters | |
|---|---|
| divider | Drawable: the drawable to use | 
setDividerHeight
void setDividerHeight (int height)
Sets the height of the divider that will be drawn between each item in the list. Calling
 this will override the intrinsic height as set by setDivider(Drawable)
Related XML Attributes:
| Parameters | |
|---|---|
| height | int: The new height of the divider in pixels. | 
setPreferenceScreen
void setPreferenceScreen (PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing.
| Parameters | |
|---|---|
| preferenceScreen | PreferenceScreen: The rootPreferenceScreenof the preference hierarchy. | 
setPreferencesFromResource
void setPreferencesFromResource (int preferencesResId, 
                String key)Inflates the given XML resource and replaces the current preference hierarchy (if any) with
 the preference hierarchy rooted at key.
| Parameters | |
|---|---|
| preferencesResId | int: The XML resource ID to inflate. | 
| key | String: The preference key of thePreferenceScreento use as the root of the preference hierarchy, or null to use the rootPreferenceScreen. | 
Protected methods
onCreateAdapter
Adapter onCreateAdapter (PreferenceScreen preferenceScreen)
Creates the root adapter.
| Parameters | |
|---|---|
| preferenceScreen | PreferenceScreen: Preference screen object to create the adapter for. | 
| Returns | |
|---|---|
| Adapter | An adapter that contains the preferences contained in this PreferenceScreen. | 
- Interfaces- DialogPreference.TargetFragment
- Preference.OnPreferenceChangeListener
- Preference.OnPreferenceClickListener
- PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback
- PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
- PreferenceFragmentCompat.OnPreferenceStartScreenCallback
- PreferenceGroup.PreferencePositionCallback
- PreferenceManager.OnDisplayPreferenceDialogListener
- PreferenceManager.OnNavigateToScreenListener
- PreferenceManager.OnPreferenceTreeClickListener
 
- Classes- CheckBoxPreference
- DialogPreference
- DropDownPreference
- EditTextPreference
- EditTextPreferenceDialogFragmentCompat
- ListPreference
- ListPreferenceDialogFragmentCompat
- MultiSelectListPreferenceDialogFragmentCompat
- Preference
- Preference.BaseSavedState
- PreferenceCategory
- PreferenceDataStore
- PreferenceDialogFragmentCompat
- PreferenceFragmentCompat
- PreferenceGroup
- PreferenceManager
- PreferenceManager.PreferenceComparisonCallback
- PreferenceManager.SimplePreferenceComparisonCallback
- PreferenceScreen
- PreferenceViewHolder
- R
- R.anim
- R.attr
- R.bool
- R.color
- R.dimen
- R.drawable
- R.id
- R.integer
- R.layout
- R.string
- R.style
- R.styleable
- SeekBarPreference
- SwitchPreferenceCompat
- TwoStatePreference
 
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
