classMyActivity:AppCompatActivity(),PreferenceFragmentCompat.OnPreferenceStartFragmentCallback{...overridefunonPreferenceStartFragment(caller:PreferenceFragmentCompat,pref:Preference):Boolean{// Instantiate the new Fragment.valargs=pref.extrasvalfragment=supportFragmentManager.fragmentFactory.instantiate(classLoader,pref.fragment)fragment.arguments=argsfragment.setTargetFragment(caller,0)// Replace the existing Fragment with the new Fragment.supportFragmentManager.beginTransaction().replace(R.id.settings_container,fragment).addToBackStack(null).commit()returntrue}}
Java
publicclassMyActivityextendsAppCompatActivityimplementsPreferenceFragmentCompat.OnPreferenceStartFragmentCallback{...@OverridepublicbooleanonPreferenceStartFragment(PreferenceFragmentCompatcaller,Preferencepref){// Instantiate the new Fragment.finalBundleargs=pref.getExtras();finalFragmentfragment=getSupportFragmentManager().getFragmentFactory().instantiate(getClassLoader(),pref.getFragment());fragment.setArguments(args);fragment.setTargetFragment(caller,0);// Replace the existing Fragment with the new Fragment.getSupportFragmentManager().beginTransaction().replace(R.id.settings_container,fragment).addToBackStack(null).commit();returntrue;}}
PreferenceScreens
使用嵌套的 XML 资源在同一 XML 资源中声明嵌套层次结构
不再支持 <PreferenceScreen>。使用嵌套的 Fragment 对象
。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Organize your settings\nPart of [Android Jetpack](/jetpack).\n===========================================================\n\nLarge and complex settings screens can make it difficult for a user to find a\nspecific setting they want to change. The Preference library offers the\nfollowing ways to better organize your settings screens.\n\nPreference categories\n---------------------\n\nIf you have several related\n[`Preference`](/jetpack/androidx/releases/preference) objects on a single\nscreen, you can group them using a\n[`PreferenceCategory`](/reference/androidx/preference/PreferenceCategory). A\n`PreferenceCategory` displays a category title and visually separates the\ncategory.\n\nTo define a `PreferenceCategory` in XML, wrap the `Preference` tags with a\n`PreferenceCategory`, as follows: \n\n```xml\n\u003cPreferenceScreen\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n\n \u003cPreferenceCategory\n app:key=\"notifications_category\"\n app:title=\"Notifications\"\u003e\n\n \u003cSwitchPreferenceCompat\n app:key=\"notifications\"\n app:title=\"Enable message notifications\"/\u003e\n\n \u003c/PreferenceCategory\u003e\n\n \u003cPreferenceCategory\n app:key=\"help_category\"\n app:title=\"Help\"\u003e\n\n \u003cPreference\n app:key=\"feedback\"\n app:summary=\"Report technical issues or suggest new features\"\n app:title=\"Send feedback\"/\u003e\n\n \u003c/PreferenceCategory\u003e\n\n\u003c/PreferenceScreen\u003e\n```\n\nThe result looks like the following:\n**Figure 1.** Preferences within categories.\n\nSplit your hierarchy into multiple screens\n------------------------------------------\n\nIf you have a large number of `Preference` objects or distinct categories, you\ncan display them on separate screens. Each screen is a\n`PreferenceFragmentCompat` with its own separate hierarchy. `Preference` objects\non your initial screen can then link to subscreens that contain related\npreferences.\n\nFigure 2 shows a simple hierarchy that contains two categories: **Messages** and\n**Sync**.\n**Figure 2.** A simple hierarchy with two categories.\n\nFigure 3 shows the same set of preferences split into multiple screens:\n**Figure 3.** A hierarchy split into multiple screens.\n\nTo link screens with a `Preference`, you can declare an `app:fragment` in XML or\nyou can use\n[`Preference.setFragment()`](/reference/androidx/preference/Preference#setFragment(java.lang.String)).\nLaunch the full package name of the `PreferenceFragmentCompat` when\nthe `Preference` is tapped, as shown in the following example: \n\n```xml\n\u003cPreference\n app:fragment=\"com.example.SyncFragment\"\n .../\u003e\n```\n\nWhen a user taps a `Preference` with an associated `Fragment`, the interface\nmethod\n[`PreferenceFragmentCompat.OnPreferenceStartFragmentCallback.onPreferenceStartFragment()`](/reference/androidx/preference/PreferenceFragmentCompat.OnPreferenceStartFragmentCallback#onPreferenceStartFragment(androidx.preference.PreferenceFragmentCompat,%20androidx.preference.Preference))\nis called. This method is where you handle displaying the new screen and where\nthe screen is implemented in the surrounding `Activity`.\n| **Note:** if you don't implement `onPreferenceStartFragment()`, a fallback implementation is used instead. While this works in most cases, we strongly recommend implementing this method so you can fully configure transitions between `Fragment` objects and update the title displayed in your `Activity` toolbar, if applicable.\n\nA typical implementation looks similar to the following: \n\n### Kotlin\n\n```kotlin\nclass MyActivity : AppCompatActivity(),\n PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {\n ...\n override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {\n // Instantiate the new Fragment.\n val args = pref.extras\n val fragment = supportFragmentManager.fragmentFactory.instantiate(\n classLoader,\n pref.fragment)\n fragment.arguments = args\n fragment.setTargetFragment(caller, 0)\n // Replace the existing Fragment with the new Fragment.\n supportFragmentManager.beginTransaction()\n .replace(R.id.settings_container, fragment)\n .addToBackStack(null)\n .commit()\n return true\n }\n}\n```\n\n### Java\n\n```java\npublic class MyActivity extends AppCompatActivity implements\n PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {\n ...\n @Override\n public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {\n // Instantiate the new Fragment.\n final Bundle args = pref.getExtras();\n final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(\n getClassLoader(),\n pref.getFragment());\n fragment.setArguments(args);\n fragment.setTargetFragment(caller, 0);\n // Replace the existing Fragment with the new Fragment.\n getSupportFragmentManager().beginTransaction()\n .replace(R.id.settings_container, fragment)\n .addToBackStack(null)\n .commit();\n return true;\n }\n}\n```\n\n### PreferenceScreens\n\nDeclaring nested hierarchies within the same XML resource using a nested\n`<PreferenceScreen>` is no longer supported. Use nested `Fragment` objects\ninstead.\n\n### Use separate Activities\n\nAlternatively, if you need to heavily customize each screen, or if you want full\n`Activity` transitions between screens, you can use a separate `Activity` for\neach `PreferenceFragmentCompat`. By doing this, you can fully customize each\n`Activity` and its corresponding settings screen. For most apps, we don't\nrecommended this; instead, use `Fragments` as previously described.\n\nFor more information about launching an `Activity` from a `Preference`, see\n[Preference actions](/guide/topics/ui/settings/customize-your-settings#actions)."]]