PreferenceFragment

public abstract class PreferenceFragment
extends Fragment implements PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment

java.lang.Object
   ↳ android.app.Fragment
     ↳ androidx.preference.PreferenceFragment


This class is deprecated.
Use PreferenceFragmentCompat instead

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 PreferenceManager.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:

  • From an XML file specifying the hierarchy
  • From different Activities that each specify its own preferences in an XML file via Activity meta-data
  • From an object hierarchy rooted with 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 building a settings screen using the AndroidX Preference library, see Settings.

Summary

Nested classes

interface PreferenceFragment.OnPreferenceDisplayDialogCallback

Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. 

interface PreferenceFragment.OnPreferenceStartFragmentCallback

Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. 

interface PreferenceFragment.OnPreferenceStartScreenCallback

Interface 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

String ARG_PREFERENCE_ROOT

This constant is deprecated. Use PreferenceFragmentCompat instead

Inherited constants

Public constructors

PreferenceFragment()

Public methods

void addPreferencesFromResource(int preferencesResId)

This method is deprecated. Use PreferenceFragmentCompat instead

<T extends Preference> T findPreference(CharSequence key)

This method is deprecated. Use PreferenceFragmentCompat instead

final RecyclerView getListView()

This method is deprecated. Use PreferenceFragmentCompat instead

PreferenceManager getPreferenceManager()

This method is deprecated. Use PreferenceFragmentCompat instead

PreferenceScreen getPreferenceScreen()

This method is deprecated. Use PreferenceFragmentCompat instead

void onCreate(Bundle savedInstanceState)
RecyclerView.LayoutManager onCreateLayoutManager()

This me