PreferenceFragmentCompat
abstract class PreferenceFragmentCompat : Fragment, PreferenceManager.OnPreferenceTreeClickListener, PreferenceManager.OnDisplayPreferenceDialogListener, PreferenceManager.OnNavigateToScreenListener, DialogPreference.TargetFragment
kotlin.Any | ||
↳ | androidx.fragment.app.Fragment | |
↳ | androidx.preference.PreferenceFragmentCompat |
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 PreferenceManager#getDefaultSharedPreferences(android.content.Context)
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(int, String)
. An example example XML resource is shown further down.
To build a hierarchy from code, use PreferenceManager#createPreferenceScreen(Context)
to create the root PreferenceScreen
. Once you have added other Preference
s to this root scree with PreferenceScreen#addPreference(Preference)
, you then need to set the screen as the root screen in your hierarchy with setPreferenceScreen(PreferenceScreen)
.
As a convenience, this fragment implements a click listener for any preference in the current hierarchy, see onPreferenceTreeClick(Preference)
.
Sample Code
The following sample code shows a simple settings screen using an XML resource. The XML resource is as follows:
The fragment that loads the XML resource is as follows:
Summary
Nested classes | |
---|---|
abstract |
Interface that the fragment's containing activity should implement to be able to process preference items that wish to display a dialog. |
abstract |
Interface that the fragment's containing activity should implement to be able to process preference items that wish to switch to a specified fragment. |
abstract |
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 | |
---|---|
static String |
Fragment argument used to specify the tag of the desired root |
Public constructors | |
---|---|
<init>() A PreferenceFragmentCompat is the entry point to using the Preference library. |
Public methods | |
---|---|
open Unit |
addPreferencesFromResource(@XmlRes preferencesResId: Int) Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy. |
open T? |
findPreference(@NonNull key: CharSequence) |
RecyclerView! | |
open PreferenceManager! |
Returns the |
open PreferenceScreen! |
Gets the root of the preference hierarchy that this fragment is showing. |
open Unit | |
open RecyclerView.LayoutManager! |
Called from |
abstract Unit |
onCreatePreferences(savedInstanceState: Bundle!, rootKey: String!) Called during |
open RecyclerView! |
onCreateRecyclerView(inflater: LayoutInflater!, parent: ViewGroup!, savedInstanceState: Bundle!) Creates the |
open View? |
onCreateView(@NonNull inflater: LayoutInflater, @Nullable container: ViewGroup?, @Nullable savedInstanceState: Bundle?) |
open Unit | |
open Unit |
onDisplayPreferenceDialog(preference: Preference!) Called when a preference in the tree requests to display a dialog. |
open Unit |
Called by |
open Boolean |
onPreferenceTreeClick(preference: Preference!) Called when a preference in the tree rooted at this |
open Unit |
onSaveInstanceState(@NonNull outState: Bundle) |
open Unit |
onStart() |
open Unit |
onStop() |
open Unit |
onViewCreated(@NonNull view: View, @Nullable savedInstanceState: Bundle?) |
open Unit |
scrollToPreference(key: String!) |
open Unit |
scrollToPreference(preference: Preference!) |
open Unit |
setDivider(divider: Drawable!) Sets the |
open Unit |
setDividerHeight(height: Int) Sets the height of the divider that will be drawn between each item in the list. |
open Unit |
setPreferenceScreen(preferenceScreen: PreferenceScreen!) Sets the root of the preference hierarchy that this fragment is showing. |
open Unit |
setPreferencesFromResource(@XmlRes preferencesResId: Int, @Nullable key: String?) Inflates the given XML resource and replaces the current preference hierarchy (if any) with the preference hierarchy rooted at |
Protected methods | |
---|---|
open RecyclerView.Adapter<RecyclerView.ViewHolder!>! |
onCreateAdapter(preferenceScreen: PreferenceScreen!) Creates the root adapter. |
Inherited functions | |
---|---|