PreferenceFragmentCompat
public abstract class PreferenceFragmentCompat extends Fragment implements PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment
BaseLeanbackPreferenceFragmentCompat |
This fragment provides a preference fragment with leanback-style behavior, suitable for embedding into broader UI elements. |
LeanbackPreferenceFragmentCompat |
This fragment provides a fully decorated leanback-style preference fragment, including a list background and header. |
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:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <PreferenceCategory android:title="@string/basic_preferences"> <Preference android:key="preference" android:title="@string/title_basic_preference" android:summary="@string/summary_basic_preference"/> <Preference android:key="stylized" android:title="@string/title_stylish_preference" android:summary="@string/summary_stylish_preference"/> <Preference android:key="icon" android:title="@string/title_icon_preference" android:summary="@string/summary_icon_preference" android:icon="@android:drawable/ic_menu_camera"/> <Preference android:key="single_line_title" android:title="@string/title_single_line_title_preference" android:summary="@string/summary_single_line_title_preference" app:singleLineTitle="true"/> </PreferenceCategory> <PreferenceCategory android:title="@string/widgets"> <CheckBoxPreference android:key="checkbox" android:title="@string/title_checkbox_preference" android:summary="@string/summary_checkbox_preference"/> <SwitchPreferenceCompat android:key="switch" android:title="@string/title_switch_preference" android:summary="@string/summary_switch_preference"/> <DropDownPreference android:key="dropdown" android:title="@string/title_dropdown_preference" android:entries="@array/entries" app:useSimpleSummaryProvider="true" android:entryValues="@array/entry_values"/> <SeekBarPreference android:key="seekbar" android:title="@string/title_seekbar_preference" android:max="10" android:defaultValue="5"/> </PreferenceCategory> <PreferenceCategory android:title="@string/dialogs"> <EditTextPreference android:key="edittext" android:title="@string/title_edittext_preference" app:useSimpleSummaryProvider="true" android:dialogTitle="@string/dialog_title_edittext_preference"/> <ListPreference android:key="list" android:title="@string/title_list_preference" app:useSimpleSummaryProvider="true" android:entries="@array/entries" android:entryValues="@array/entry_values" android:dialogTitle="@string/dialog_title_list_preference"/> <MultiSelectListPreference android:key="multi_select_list" android:title="@string/title_multi_list_preference" android:summary="@string/summary_multi_list_preference" android:entries="@array/entries" android:entryValues="@array/entry_values" android:dialogTitle="@string/dialog_title_multi_list_preference"/> </PreferenceCategory> <PreferenceCategory android:key="advanced" android:title="@string/advanced_attributes" app:initialExpandedChildrenCount="1"> <Preference android:key="expandable" android:title="@string/title_expandable_preference" android:summary="@string/summary_expandable_preference"/> <Preference 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"/> </Preference> <SwitchPreferenceCompat android:key="parent" android:title="@string/title_parent_preference" android:summary="@string/summary_parent_preference"/> <SwitchPreferenceCompat android:key="child" android:dependency="parent" android:title="@string/title_child_preference" android:summary="@string/summary_child_preference"/> <SwitchPreferenceCompat android:key="toggle_summary" android:title="@string/title_toggle_summary_preference" android:summaryOn="@string/summary_on_toggle_summary_preference" android:summaryOff="@string/summary_off_toggle_summary_preference"/> <Preference android:key="copyable" android:title="@string/title_copyable_preference" android:summary="@string/summary_copyable_preference" android:selectable="false" app:enableCopying="true"/> </PreferenceCategory> </PreferenceScreen>
The fragment that loads the XML resource is as follows:
public static class DemoFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { // Load the preferences from an XML resource setPreferencesFromResource(R.xml.preferences, rootKey); } }
| See also | |
|---|---|
Preference |
|
PreferenceScreen |
Summary
Nested types |
|---|
public interface PreferenceFragmentCompat.OnPreferenceDisplayDialogCallbackInterface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. |
public interface PreferenceFragmentCompat.OnPreferenceStartFragmentCallbackInterface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. |
public interface PreferenceFragmentCompat.OnPreferenceStartScreenCallbackInterface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a new screen of preferences. |
Constants |
|
|---|---|
static final String |
ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"Fragment argument used to specify the tag of the desired root |
Public constructors |
|---|
Public methods |
|
|---|---|
void |
addPreferencesFromResource(@XmlRes int preferencesResId)Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy. |
@Nullable T |
<T extends Preference> findPreference(@NonNull CharSequence key)Finds a |
final RecyclerView |
|
PreferenceManager |
Returns the |
PreferenceScreen |
Gets the root of the preference hierarchy that this fragment is showing. |
void |
Called to do initial creation of a fragment. |
@NonNull RecyclerView.LayoutManager |
Called from |
abstract void |
onCreatePreferences(Called during |
@NonNull RecyclerView |
onCreateRecyclerView(Creates the |
@NonNull View |
onCreateView(Called to have the fragment instantiate its user interface view. |
void |
Called when the view previously created by |
void |
onDisplayPreferenceDialog(@NonNull Preference preference)Called when a preference in the tree requests to display a dialog. |
void |
onNavigateToScreen(@NonNull PreferenceScreen preferenceScreen)Called by |
boolean |
onPreferenceTreeClick(@NonNull Preference preference)Called when a preference in the tree rooted at this has been clicked. |
void |
onSaveInstanceState(@NonNull Bundle outState)Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if 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(@NonNull View view, @Nullable Bundle savedInstanceState)Called immediately after |
void |
scrollToPreference(@NonNull String key) |
void |
scrollToPreference(@NonNull Preference preference) |
void |
setDivider(@Nullable Drawable divider)Sets the |
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(Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at |
Protected methods |
|
|---|---|
@NonNull RecyclerView.Adapter |
onCreateAdapter(@NonNull PreferenceScreen preferenceScreen)Creates the root adapter. |
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
ARG_PREFERENCE_ROOT
public static final String ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT"
Fragment argument used to specify the tag of the desired root PreferenceScreen object.
Public constructors
Public methods
addPreferencesFromResource
public void addPreferencesFromResource(@XmlRes int preferencesResId)
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
| Parameters | |
|---|---|
@XmlRes int preferencesResId |
The XML resource ID to inflate |
findPreference
public @Nullable T <T extends Preference> findPreference(@NonNull CharSequence key)
Finds a Preference with the given key. Returns null if no Preference could be found with the given key.
| Parameters | |
|---|---|
@NonNull CharSequence key |
The key of the |
| Returns | |
|---|---|
@Nullable T |
The |
| See also | |
|---|---|
findPreference |
getPreferenceManager
public PreferenceManager getPreferenceManager()
Returns the PreferenceManager used by this fragment.
| Returns | |
|---|---|
PreferenceManager |
The |
getPreferenceScreen
public PreferenceScreen getPreferenceScreen()
Gets the root of the preference hierarchy that this fragment is showing.
| Returns | |
|---|---|
PreferenceScreen |
The |
onCreate
public void onCreate(@Nullable Bundle savedInstanceState)
Called to do initial creation of a fragment. This is called after onAttach and before onCreateView.
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, add a androidx.lifecycle.LifecycleObserver on the activity's Lifecycle, removing it when it receives the CREATED callback.
Any restored child fragments will be created before the base Fragment.onCreate method returns.
onCreateLayoutManager
public @NonNull RecyclerView.LayoutManager onCreateLayoutManager()
Called from onCreateRecyclerView to create the RecyclerView.LayoutManager for the created RecyclerView.
| Returns | |
|---|---|
@NonNull RecyclerView.LayoutManager |
A new |
onCreatePreferences
public abstract void onCreatePreferences(
@Nullable Bundle savedInstanceState,
@Nullable String rootKey
)
Called during onCreate to supply the preferences for this fragment. Subclasses are expected to call setPreferenceScreen either directly or via helper methods such as addPreferencesFromResource.
| Parameters | |
|---|---|
@Nullable Bundle savedInstanceState |
If the fragment is being re-created from a previous saved state, this is the state. |
@Nullable String rootKey |
If non-null, this preference fragment should be rooted at the |
onCreateRecyclerView
public @NonNull RecyclerView onCreateRecyclerView(
@NonNull LayoutInflater inflater,
@NonNull ViewGroup parent,
@Nullable Bundle savedInstanceState
)
Creates the RecyclerView used to display the preferences. Subclasses may override this to return a customized RecyclerView.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The LayoutInflater object that can be used to inflate the |
@NonNull ViewGroup parent |
The parent |
@Nullable Bundle savedInstanceState |
If non-null, this view is being re-constructed from a previous saved state as given here. |
| Returns | |
|---|---|
@NonNull RecyclerView |
A new |
onCreateView
public @NonNull View onCreateView(
@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState
)
Called to have the fragment instantiate its user interface view. This is optional, and non-graphical fragments can return null. This will be called between onCreate and onViewCreated.
A default View can be returned by calling Fragment in your constructor. Otherwise, this method returns null.
It is recommended to only inflate the layout in this method and move logic that operates on the returned View to onViewCreated.
If you return a View from here, you will later be called in onDestroyView when the view is being released.
| Parameters | |
|---|---|
@NonNull LayoutInflater inflater |
The LayoutInflater object that can be used to inflate any views in the fragment, |
@Nullable ViewGroup container |
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. |
@Nullable Bundle savedInstanceState |
If non-null, this fragment is being re-constructed from a previous saved state as given here. |
onDestroyView
public void onDestroyView()
Called when the view previously created by onCreateView 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 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
public void onDisplayPreferenceDialog(@NonNull 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 | |
|---|---|
@NonNull Preference preference |
The |
onNavigateToScreen
public void onNavigateToScreen(@NonNull PreferenceScreen preferenceScreen)
Called by onClick in order to navigate to a new screen of preferences. Calls onPreferenceStartScreen if the target fragment or containing activity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback.
| Parameters | |
|---|---|
@NonNull PreferenceScreen preferenceScreen |
The |
onPreferenceTreeClick
public boolean onPreferenceTreeClick(@NonNull Preference preference)
Called when a preference in the tree rooted at this has been clicked.
onSaveInstanceState
public void onSaveInstanceState(@NonNull Bundle outState)
Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if 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, onCreateView, and onViewCreated.
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.
onStart
public 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
public void onStop()
Called when the Fragment is no longer started. This is generally tied to Activity.onStop of the containing Activity's lifecycle.
onViewCreated
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
Called immediately after onCreateView 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 | |
|---|---|
@NonNull View view |
The View returned by |
@Nullable Bundle savedInstanceState |
If non-null, this fragment is being re-constructed from a previous saved state as given here. |
setDivider
public void setDivider(@Nullable 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.
setDividerHeight
public 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.
| Parameters | |
|---|---|
int height |
The new height of the divider in pixels |
setPreferenceScreen
public void setPreferenceScreen(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing.
| Parameters | |
|---|---|
PreferenceScreen preferenceScreen |
The root |
setPreferencesFromResource
public void setPreferencesFromResource(
@XmlRes int preferencesResId,
@Nullable String key
)
Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at key.
| Parameters | |
|---|---|
@XmlRes int preferencesResId |
The XML resource ID to inflate |
@Nullable String key |
The preference key of the |
Protected methods
onCreateAdapter
protected @NonNull RecyclerView.Adapter onCreateAdapter(@NonNull PreferenceScreen preferenceScreen)
Creates the root adapter.
| Parameters | |
|---|---|
@NonNull PreferenceScreen preferenceScreen |
The |
| Returns | |
|---|---|
@NonNull RecyclerView.Adapter |
An adapter that contains the preferences contained in this |