PreferenceFragment
public
abstract
class
PreferenceFragment
extends Fragment
java.lang.Object | ||
↳ | android.app.Fragment | |
↳ | android.preference.PreferenceFragment |
This class was deprecated
in API level 28.
Use the AndroidX
Preference Library for consistent behavior across all devices. For more information on
using the AndroidX Preference Library see
Settings.
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 showing these other screens from the preference hierarchy.
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 Intent
to query Activities
that each
have preferences, use addPreferencesFromIntent(Intent)
. Each
Activity
can specify meta-data in the manifest (via the key
PreferenceManager#METADATA_KEY_PREFERENCES
) that points to an XML
resource. These XML resources will be inflated into a single preference
hierarchy and shown by this fragment.
To specify an object hierarchy rooted with PreferenceScreen
, use
setPreferenceScreen(android.preference.PreferenceScreen)
.
As a convenience, this fragment implements a click listener for any
preference in the current hierarchy, see
onPreferenceTreeClick(android.preference.PreferenceScreen, android.preference.Preference)
.
Developer Guides
For information about using PreferenceFragment
,
read the Settings
guide.
See also:
Summary
Nested classes | |
---|---|
interface |
PreferenceFragment.OnPreferenceStartFragmentCallback
This interface was deprecated
in API level 28.
Use |
Inherited XML attributes | |
---|---|
Inherited constants |
---|
Public constructors | |
---|---|
PreferenceFragment()
|
Public methods | |
---|---|
void
|
addPreferencesFromIntent(Intent intent)
Adds preferences from activities that match the given |
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 |
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
|
onActivityResult(int requestCode, int resultCode, Intent data)
Receive the result from a previous call to
|
void
|
onCreate(Bundle savedInstanceState)
Called to do initial creation of a fragment. |
View
|
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Called to have the fragment instantiate its user interface view. |
void
|
onDestroy()
Called when the fragment is no longer in use. |
void
|
onDestroyView()
Called when the view previously created by |
boolean
|
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
|
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
|
setPreferenceScreen(PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing. |
Inherited methods | |
---|---|
Public constructors
PreferenceFragment
public PreferenceFragment ()
Public methods
addPreferencesFromIntent
public void addPreferencesFromIntent (Intent intent)
Adds preferences from activities that match the given Intent
.
Parameters | |
---|---|
intent |
Intent : The Intent to query activities. |
addPreferencesFromResource
public 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
public Preference findPreference (CharSequence key)
Finds a Preference
based on its key.
Parameters | |
---|---|
key |
CharSequence : The key of the preference to retrieve. |
Returns | |
---|---|
Preference |
The Preference with the key, or null. |
getPreferenceManager
public PreferenceManager getPreferenceManager ()
Returns the PreferenceManager
used by this fragment.
Returns | |
---|---|
PreferenceManager |
The PreferenceManager . |
getPreferenceScreen
public PreferenceScreen getPreferenceScreen ()
Gets the root of the preference hierarchy that this fragment is showing.
Returns | |
---|---|
PreferenceScreen |
The PreferenceScreen that is the root of the preference
hierarchy. |
onActivityCreated
public 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(android.os.Bundle)
.
If you override this method you must call through to the
superclass implementation.
Parameters | |
---|---|
savedInstanceState |
Bundle : This value may be null . |
onActivityResult
public void onActivityResult (int requestCode, int resultCode, Intent data)
Receive the result from a previous call to
startActivityForResult(android.content.Intent, int)
. This follows the
related Activity API as described there in
Activity#onActivityResult(int, int, Intent)
.
Parameters | |
---|---|
requestCode |
int : The integer request code originally supplied to
startActivityForResult(), allowing you to identify who this
result came from. |
resultCode |
int : The integer result code returned by the child activity
through its setResult(). |
data |
Intent : An Intent, which can return result data to the caller
(various data can be attached to Intent "extras"). |
onCreate
public void onCreate (Bundle savedInstanceState)
Called to do initial creation of a fragment. This is called after
onAttach(android.app.Activity)
and before
onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
, but is not called if the fragment
instance is retained across Activity re-creation (see setRetainInstance(boolean)
).
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(android.os.Bundle)
.
If your app's targetSdkVersion
is Build.VERSION_CODES.M
or lower, child fragments being restored from the savedInstanceState are restored after
onCreate
returns. When targeting Build.VERSION_CODES.N
or
above and running on an N or newer platform version
they are restored by Fragment.onCreate
.
If you override this method you must call through to the superclass implementation.
Parameters | |
---|---|
savedInstanceState |
Bundle : This value may be null . |
onCreateView
public 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(android.os.Bundle)
and onActivityCreated(android.os.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 : This value may be null . |
savedInstanceState |
Bundle : This value may be null . |
Returns | |
---|---|
View |
Return the View for the fragment's UI, or null. |
onDestroy
public void onDestroy ()
Called when the fragment is no longer in use. This is called
after onStop()
and before onDetach()
.
If you override this method you must call through to the
superclass implementation.
onDestroyView
public 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.
If you override this method you must call through to the
superclass implementation.
onPreferenceTreeClick
public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference)
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen |
preference |
Preference |
Returns | |
---|---|
boolean |
onSaveInstanceState
public 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(android.os.Bundle)
,
onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
, and
onActivityCreated(android.os.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
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.
If you override this method you must call through to the
superclass implementation.
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.
If you override this method you must call through to the
superclass implementation.
onViewCreated
public void onViewCreated (View view, Bundle savedInstanceState)
Called immediately after onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.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 by onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) . |
savedInstanceState |
Bundle : This value may be null . |
setPreferenceScreen
public void setPreferenceScreen (PreferenceScreen preferenceScreen)
Sets the root of the preference hierarchy that this fragment is showing.
Parameters | |
---|---|
preferenceScreen |
PreferenceScreen : The root PreferenceScreen of the preference hierarchy. |
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 2024-06-18 UTC.