public abstract class PreferenceFragment extends Fragment
Preferenceobjects as lists. These preferences will automatically save to
SharedPreferencesas the user interacts with them. To retrieve an instance of
SharedPreferencesthat 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.
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:
Activitiesthat each specify its own preferences in an XML file via
To inflate from XML, use the
root element should be a
PreferenceScreen. Subsequent elements can point
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
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.
As a convenience, this fragment implements a click listener for any
preference in the current hierarchy, see
For information about using
read the Settings
The following sample code shows a simple preference fragment that is populated from a resource. The resource it loads is:
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:
|Modifier and Type||Class and Description|
Interface that PreferenceFragment's containing activity should implement to be able to process preference items that wish to switch to a new fragment.
|Constructor and Description|
|Modifier and Type||Method and Description|
Adds preferences from activities that match the given
Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.
Gets the root of the preference hierarchy that this fragment is showing.
Called when the fragment's activity has been created and this fragment's view hierarchy instantiated.
Receive the result from a previous call to
Called to do initial creation of a fragment.
Called to have the fragment instantiate its user interface view.
Called when the fragment is no longer in use.
Called when the view previously created by
Called when a preference in the tree rooted at this
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.
Called when the Fragment is visible to the user.
Called when the Fragment is no longer started.
Sets the root of the preference hierarchy that this fragment is showing.
dump, equals, getActivity, getArguments, getChildFragmentManager, getFragmentManager, getId, getLayoutInflater, getLoaderManager, getParentFragment, getResources, getRetainInstance, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, hashCode, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isRemoving, isResumed, isVisible, onAttach, onConfigurationChanged, onContextItemSelected, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onDestroyOptionsMenu, onDetach, onHiddenChanged, onInflate, onInflate, onLowMemory, onOptionsItemSelected, onOptionsMenuClosed, onPause, onPrepareOptionsMenu, onResume, onTrimMemory, onViewCreated, onViewStateRestored, registerForContextMenu, setArguments, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setRetainInstance, setTargetFragment, setUserVisibleHint, startActivity, startActivity, startActivityForResult, startActivityForResult, toString, unregisterForContextMenu
public void onCreate(Bundle savedInstanceState)
Fragment.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
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
If you return a View from here, you will later be called in
Fragment.onDestroyView() when the view is being released.
inflater- The LayoutInflater object that can be used to inflate any views in the fragment,
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.
savedInstanceState- If non-null, this fragment is being re-constructed from a previous saved state as given here.
public void onActivityCreated(Bundle savedInstanceState)
Fragment.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
Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)and before
public void onStart()
Activity.onStartof the containing Activity's lifecycle.
public void onStop()
Activity.onStopof the containing Activity's lifecycle.
public void onDestroyView()
Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.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
Fragment.onDestroy(). It is called regardless of whether
Fragment.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.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.
public void onDestroy()
public void onSaveInstanceState(Bundle outState)
Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle), and
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
Fragment.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.
public void onActivityResult(int requestCode, int resultCode, Intent data)
Fragment.startActivityForResult(Intent, int). This follows the related Activity API as described there in
Activity.onActivityResult(int, int, Intent).
requestCode- The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from.
resultCode- The integer result code returned by the child activity through its setResult().
data- An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
public PreferenceManager getPreferenceManager()
PreferenceManagerused by this fragment.
public void setPreferenceScreen(PreferenceScreen preferenceScreen)
preferenceScreen- The root
PreferenceScreenof the preference hierarchy.
public PreferenceScreen getPreferenceScreen()
PreferenceScreenthat is the root of the preference hierarchy.
public void addPreferencesFromIntent(Intent intent)
Intentto query activities.
public void addPreferencesFromResource(int preferencesResId)
preferencesResId- The XML resource ID to inflate.
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
PreferenceScreenhas been clicked.
PreferenceScreenthat the preference is located in.
preference- The preference that was clicked.
public Preference findPreference(CharSequence key)
Preferencebased on its key.
public ListView getListView()