2

I am having a problem with PreferenceFragmentCompat while trying to develop nested settings for my android application. I have Fragment called Settings Fragment where I have following method:

@Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        Log.d("KEY", rootKey + "KEY");
        setPreferencesFromResource(R.xml.pref_main, rootKey);
    }

However, when I run the application I always get null to Logcat. Here is my pref_main.xml which have nested PreferencesScreen.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="test">
    <PreferenceScreen
        android:key="button_voicemail_category_key"
        android:title="test"
        android:persistent="false">
        <EditTextPreference
            android:defaultValue="Default value"
            android:key="edit_text_preference_1"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:title="Edit text preference" />
    </PreferenceScreen>

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="check_box_preference_1"
        android:title="Check box preference" />
</PreferenceScreen>

Do you have any idea where the problem lies, because I tried many different things but couldn't find a solution for the problem.

I am looking forward to your response!

modivno
  • 37
  • 7
  • You may want to take a look at [this question](https://stackoverflow.com/questions/32070186/how-to-use-the-v7-v14-preference-support-library), make sure you're inheriting from AppCompatActivity, etc. What exactly do you mean by "I always get null to Logcat?" Can you post the logcat? – greeble31 Sep 10 '18 at 13:38

1 Answers1

0

You probably misunderstood the meaning of the rootKey. Reading the documentation it says

String: If non-null, this preference fragment should be rooted at the PreferenceScreen with this key.

That means if your PreferenceScreen is your main PreferenceScreen, in your case the Screen with the key attribute test, the rootKey is set to null. For the child screens, which gonna be open differently, the rootKey will be set to test, since those are the childs of the PreferenceScreen with the key test.

However, your should have a look on the current guide. It says:

Declaring nested hierarchies within the same XML resource using a nested <PreferenceScreen> is no longer supported. You should use nested Fragment objects instead.

See also: onCreatePreferences and Organizing your settings

malliaridis
  • 389
  • 1
  • 12