Added in API level 1
Deprecated in API level 29

PreferenceActivity


abstract class PreferenceActivity : ListActivity, PreferenceFragment.OnPreferenceStartFragmentCallback
kotlin.Any
   ↳ android.content.Context
   ↳ android.content.ContextWrapper
   ↳ android.view.ContextThemeWrapper
   ↳ android.app.Activity
   ↳ android.app.ListActivity
   ↳ android.preference.PreferenceActivity

This is the base class for an activity to show a hierarchy of preferences to the user. Prior to android.os.Build.VERSION_CODES#HONEYCOMB this class only allowed the display of a single set of preference; this functionality should now be found in the new PreferenceFragment class. If you are using PreferenceActivity in its old mode, the documentation there applies to the deprecated APIs here.

This activity shows one or more headers of preferences, each of which is associated with a PreferenceFragment to display the preferences of that header. The actual layout and display of these associations can however vary; currently there are two major approaches it may take:

  • On a small screen it may display only the headers as a single list when first launched. Selecting one of the header items will only show the PreferenceFragment of that header (on Android N and lower a new Activity is launched).
  • On a large screen it may display both the headers and current PreferenceFragment together as panes. Selecting a header item switches to showing the correct PreferenceFragment for that item.

Subclasses of PreferenceActivity should implement onBuildHeaders to populate the header list with the desired items. Doing this implicitly switches the class into its new "headers + fragments" mode rather than the old style of just showing a single preferences list.

Summary

Nested classes

Description of a single Header item that the user can select.

Constants
static String

When starting this activity, the invoking Intent can contain this extra boolean that the header list should not be displayed.

static String

When starting this activity, the invoking Intent can contain this extra string to specify which fragment should be initially displayed.

static String

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specified to supply a Bundle of arguments to pass to that fragment when it is instantiated during the initial creation of PreferenceActivity.

static String

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specify to supply the short title to be shown for that fragment.

static String

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specify to supply the title to be shown for that fragment.

static Long

Default value for Header.id indicating that no identifier value is set.

Inherited constants
Public constructors

Public methods
open Unit

Adds preferences from activities that match the given Intent.

open Unit
addPreferencesFromResource(preferencesResId: Int)

Inflates the given XML resource and adds the preference hierarchy to the current preference hierarchy.

open Preference!

Finds a Preference based on its key.

open Unit
finishPreferencePanel(caller: Fragment!, resultCode: Int, resultData: Intent!)

Called by a preference panel fragment to finish itself.

open PreferenceManager!

Returns the PreferenceManager used by this activity.

open PreferenceScreen!

Gets the root of the preference hierarchy that this activity is showing.

open Boolean

Returns true if this activity is currently showing the header list.

open Unit

Call when you need to change the headers being displayed.

open Boolean

Returns true if this activity is showing multiple panes -- the headers and a preference fragment.

open Unit

Parse the given XML file as a header description, adding each parsed Header into the target list.

open Unit

Called when the activity has detected the user's press of the back key.

open Unit

Called when the activity needs its list of headers build.

open Intent!
onBuildStartFragmentIntent(fragmentName: String!, args: Bundle!, titleRes: Int, shortTitleRes: Int)

Called by startWithFragment(String,Bundle,Fragment,int,int,int) when in single-pane mode, to build an Intent to launch a new activity showing the selected fragment.

open Unit

Updates the screen state (current list and other views) when the content changes.

open PreferenceActivity.Header!

Called to determine the initial header to be shown.

open PreferenceActivity.Header!

Called after the header list has been updated (onBuildHeaders has been called and returned due to invalidateHeaders()) to specify the header that should now be selected.

open Unit

Called when the user selects an item in the header list.

open Boolean

Called to determine whether the header list should be hidden.

open Boolean

Called to determine if the activity should run in multi-pane mode.

open Boolean

This hook is called whenever an item in your options menu is selected.

open Boolean

Called when the user has clicked on a Preference that has a fragment class name associated with it.

open Boolean
onPreferenceTreeClick(preferenceScreen: PreferenceScreen!, preference: Preference!)

open Unit

Set a footer that should be shown at the bottom of the header list.

open Unit
setParentTitle(title: CharSequence!, shortTitle: CharSequence!, listener: View.OnClickListener!)

Should be called after onCreate to ensure that the breadcrumbs, if any, were created.

open Unit

Sets the root of the preference hierarchy that this activity is showing.

open Unit
showBreadCrumbs(title: CharSequence!, shortTitle: CharSequence!)

Change the base title of the bread crumbs for the current preferences.

open Unit

Start a new fragment.

open Unit
startPreferencePanel(fragmentClass: String!, args: Bundle!, titleRes: Int, titleText: CharSequence!, resultTo: Fragment!, resultRequestCode: Int)

Start a new fragment containing a preference panel.

open Unit
startWithFragment(fragmentName: String!, args: Bundle!, resultTo: Fragment!, resultRequestCode: Int)

Like startWithFragment(String,Bundle,Fragment,int,int,int) but uses a 0 titleRes.

open Unit
startWithFragment(fragmentName: String!, args: Bundle!, resultTo: Fragment!, resultRequestCode: Int, titleRes: Int, shortTitleRes: Int)

Start a new instance of this activity, showing only the given preference fragment.

open Unit

When in two-pane mode, switch to the fragment pane to show the given preference fragment.

open Unit
switchToHeader(fragmentName: String!, args: Bundle!)

When in two-pane mode, switch the fragment pane to show the given preference fragment.

Protected methods
open Boolean
isValidFragment(fragmentName: String!)

Subclasses should override this method and verify that the given fragment is a valid type to be attached to this activity.

open Unit
onActivityResult(requestCode: Int, resultCode: Int, data: Intent!)

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it.

open Unit
onCreate(savedInstanceState: Bundle?)

Called when the activity is starting.

open Unit

Perform any final cleanup before an activity is destroyed.

open Unit
onListItemClick(l: ListView!, v: View!, position: Int, id: Long)

This method will be called when an item in the list is selected.

open Unit
onNewIntent(intent: Intent!)

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent.FLAG_ACTIVITY_SINGLE_TOP flag when calling #startActivity.

open Unit

Ensures the list view has been created before Activity restores all of the view states.

open Unit

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in #onCreate or #onRestoreInstanceState (the Bundle populated by this method will be passed to both).

open Unit

Called when you are no longer visible to the user.

Inherited functions
Inherited properties

Constants

EXTRA_NO_HEADERS

Added in API level 11
Deprecated in API level 29
static val EXTRA_NO_HEADERS: String

Deprecated: Deprecated in Java.

When starting this activity, the invoking Intent can contain this extra boolean that the header list should not be displayed. This is most often used in conjunction with EXTRA_SHOW_FRAGMENT to launch the activity to display a specific fragment that the user has navigated to.

Value: ":android:no_headers"

EXTRA_SHOW_FRAGMENT

Added in API level 11
Deprecated in API level 29
static val EXTRA_SHOW_FRAGMENT: String

Deprecated: Deprecated in Java.

When starting this activity, the invoking Intent can contain this extra string to specify which fragment should be initially displayed.

Starting from Key Lime Pie, when this argument is passed in, the PreferenceActivity will call isValidFragment() to confirm that the fragment class name is valid for this activity.
Value: ":android:show_fragment"

EXTRA_SHOW_FRAGMENT_ARGUMENTS

Added in API level 11
Deprecated in API level 29
static val EXTRA_SHOW_FRAGMENT_ARGUMENTS: String

Deprecated: Deprecated in Java.

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specified to supply a Bundle of arguments to pass to that fragment when it is instantiated during the initial creation of PreferenceActivity.

Value: ":android:show_fragment_args"

EXTRA_SHOW_FRAGMENT_SHORT_TITLE

Added in API level 14
Deprecated in API level 29
static val EXTRA_SHOW_FRAGMENT_SHORT_TITLE: String

Deprecated: Deprecated in Java.

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specify to supply the short title to be shown for that fragment.

Value: ":android:show_fragment_short_title"

EXTRA_SHOW_FRAGMENT_TITLE

Added in API level 14
Deprecated in API level 29
static val EXTRA_SHOW_FRAGMENT_TITLE: String

Deprecated: Deprecated in Java.

When starting this activity and using EXTRA_SHOW_FRAGMENT, this extra can also be specify to supply the title to be shown for that fragment.

Value: ":android:show_fragment_title"

HEADER_ID_UNDEFINED

Added in API level 11
Deprecated in API level 29
static val HEADER_ID_UNDEFINED: Long

Deprecated: Deprecated in Java.

Default value for Header.id indicating that no identifier value is set. All other values (including those below -1) are valid.

Value: -1L

Public constructors

PreferenceActivity

PreferenceActivity()

Public methods

addPreferencesFromIntent

Added in API level 1
Deprecated in API level 15
open fun addPreferencesFromIntent(intent: Intent!): Unit

Deprecated: This function is not relevant for a modern fragment-based PreferenceActivity.

Adds preferences from activities that match the given Intent.

Parameters
intent Intent!: The Intent to query activities.

addPreferencesFromResource

Added in API level 1
Deprecated in API level 15
open fun addPreferencesFromResource(preferencesResId: Int): Unit

Deprecated: This function is not relevant for a modern fragment-based PreferenceActivity.

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

Added in API level 1
Deprecated in API level 15
open fun findPreference(key: CharSequence!): Preference!

Deprecated: This function is not relevant for a modern fragment-based PreferenceActivity.

Finds a Preference based on its key.

Parameters
key CharSequence!: The key of the preference to retrieve.
Return
Preference! The Preference with the key, or null.

finishPreferencePanel

Added in API level 11
Deprecated in API level 29
open fun finishPreferencePanel(
    caller: Fragment!,
    resultCode: Int,
    resultData: Intent!
): Unit

Deprecated: Deprecated in Java.

Called by a preference panel fragment to finish itself.

Parameters
caller Fragment!: The fragment that is asking to be finished.
resultCode Int: Optional result code to send back to the original launching fragment.
resultData Intent!: Optional result data to send back to the original launching fragment.

getPreferenceManager

Added in API level 1
Deprecated in API level 15
open fun getPreferenceManager(): PreferenceManager!

Deprecated: This function is not relevant for a modern fragment-based PreferenceActivity.

Returns the PreferenceManager used by this activity.

Return
PreferenceManager! The PreferenceManager.

getPreferenceScreen

Added in API level 1
Deprecated in API level 15
open fun getPreferenceScreen(): PreferenceScreen!

Deprecated: This function is not relevant for a modern fragment-based PreferenceActivity.

Gets the root of the preference hierarchy that this activity is showing.

Return
PreferenceScreen! The PreferenceScreen that is the root of the preference hierarchy.