Question

I set some preferences up in an xml file and used the standard constructs (see code below) to make them available and updatable and all has been working fine. As these were for test purposes I now want to replace them with my actual app preferences (including completely removing some) but whatever I do it seems only to recognise the original ones I setup. I have even completely replaced the xml file with my new one - still it only finds the old preferences. I have also cleared the entries as shown in the code (map size is 0 after the commit()). Also it does not appear to let me change the xml filename from 'preferences.xml'. The code shows my new name of preferences_new.xml but this raises an error in Eclipse.

In calling activity onCreate:

        // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new PrefsFragment()).commit();

In PrefsFragment:

        PreferenceManager pm = this.getPreferenceManager();
    SharedPreferences sp = pm.getSharedPreferences();
    Editor e = sp.edit();
    e.clear();
    e.commit();
    Map <String, ?> map = sp.getAll();
    int size = map.size();
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences_new);

Old XML file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" >
    <EditTextPreference
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:key="edittext_preference"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />

    <ListPreference
        android:dialogTitle="@string/dialog_title_list_preference"
        android:entries="@array/entries_list_preference"
        android:entryValues="@array/entryvalues_list_preference"
        android:key="list_preference"
        android:summary="@string/summary_list_preference"
        android:title="@string/title_list_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/launch_preferences" >

    <!--
         This PreferenceScreen tag serves as a screen break (similar to page break
         in word processing). Like for other preference types, we assign a key
         here so it is able to save and restore its instance state.
    -->
    <PreferenceScreen
        android:key="screen_preference"
        android:summary="@string/summary_screen_preference"
        android:title="@string/title_screen_preference" >

        <!-- You can place more preferences here that will be shown on the next screen. -->

        <CheckBoxPreference
            android:key="next_screen_checkbox_preference"
            android:summary="@string/summary_next_screen_toggle_preference"
            android:title="@string/title_next_screen_toggle_preference" />
    </PreferenceScreen>
    <PreferenceScreen
        android:summary="@string/summary_intent_preference"
        android:title="@string/title_intent_preference" >
        <intent
            android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
    </PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/preference_attributes" >
    <CheckBoxPreference
        android:key="parent_checkbox_preference"
        android:summary="@string/summary_parent_preference"
        android:title="@string/title_parent_preference" />

    <!-- The visual style of a child is defined by this styled theme attribute. -->
    <CheckBoxPreference
        android:dependency="parent_checkbox_preference"
        android:key="child_checkbox_preference"
        android:layout="?android:attr/preferenceLayoutChild"
        android:summary="@string/summary_child_preference"
        android:title="@string/title_child_preference" />
</PreferenceCategory>

New xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" 
                android:key="edittext_category" >
    <EditTextPreference
        android:id="@+id/my_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:defaultValue=""
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:inputType="phone"
        android:key="edittext_preference"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />
</PreferenceCategory>

I am almost at the 'tearing the hair out' stage, so any help would be greatly appreciated

Was it helpful?

Solution

I now think the reason why I couldn't replace the preferences file was that R.java was not being re-generated for some reason that was hard to work out. I gave up investigating that (following here) and went back to my previous working version and re-applied changes until just before the preference work to get my app back working (shows the importance of keeping versions of your code). This was a much faster alternative for me.

I have now implemented my preferences a different way (using the example here) and have dropped the use of PreferenceFragment. So, thanks to Marco for keeping me sane. I can now move on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top