In this example XML Layout of a PreferenceScreen:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="first_preferencescreen">
<CheckBoxPreference
android:key="wifi enabled"
android:title="WiFi" />
<PreferenceScreen
android:key="second_preferencescreen"
android:title="WiFi settings">
<CheckBoxPreference
android:key="prefer wifi"
android:title="Prefer WiFi" />
... other preferences here ...
</PreferenceScreen>
</PreferenceScreen>
I noticed an android:key for both PreferenceScreens (nested in each other).
I couldn’t find, however, any use for them in application code: The only keys used to access a value by program code are the ones of the actual elements: CheckBoxPreference, EditTextPreference, etc.
Can they be utilized for specifying in code “access the CheckBoxPreference that’s in this PreferenceScreen and not the other”? i.e. hierarchical access?
If not, what is the purpose of the key for a PreferenceScreen? Must it be defined? Must it be unique?
It is XML attribute belonging to superclass named Preference. It is documented
http://developer.android.com/reference/android/preference/Preference.html#attr_android:key
The application can have some use of the key, say if it is listening to changes in particular preferences like:
onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
Yes. The key purpose and value might be restricted a lot, it is certainly unique. But to define how unique and in what scope, will need more reading. (I did not look at more that just few pages of online docs)