I am using AndroidX's Preference library to manage my app's settings. My SettingsFragment derives from PreferenceFragmentCompat and loads the following layout:
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen
android:key="screen_backup_key"
android:title="@string/local_database">
<androidx.preference.Preference
android:key="button_save_key"
android:title="@string/export" />
<androidx.preference.Preference
android:key="button_load_key"
android:title="@string/_import" />
</androidx.preference.PreferenceScreen>
<androidx.preference.Preference
android:key="screen_about"
android:title="@string/about" />
</androidx.preference.PreferenceScreen>
The layout is displayed correctly, but when I click on the nested PreferenceScreen entry nothing happens. It worked before with the PreferenceScreen from the Support Library, which is now deprecated.
I would expect to see the contents to this nested PreferenceScreen. What am I doing wrong?
It is not supported anymore in AndroidX's Preference Library.
Declaring nested hierarchies within the same XML resource using a nested
PreferenceScreenis no longer supported. You should use nested Fragment objects instead.
You need a separate PreferenceFragmentCompat for each separate screen.
If you have a large number of Preferences or distinct categories, you can display them on separate screens. Each screen should be a
PreferenceFragmentCompatwith its own separate hierarchy. Preferences on your initial screen can then link to subscreens that contain related Preferences.
You do it like this:
To link screens with a Preference, you can declare an
app:fragmentin XML, or you can usePreference.setFragment(). Set the full package name of thePreferenceFragmentCompatthat should be launched when thePreferenceis tapped, as shown below:
<Preference
app:fragment="com.example.SyncFragment"
.../>
Here is the source for further reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With