3

I have a preference fragment which has some EditTextPreference items on them. Every time i load the preferences fragment the values displayed are the default values and one the ones stored in sharedPreferences. How can i get these edit texts to always display the text from the sharedPreferences when the menu loads? I tried accessing the sharedPreferences from the fragment using:

SharedPreferences def = getActivity().getPreferences(0);
Log.v("Something", def.getString("relay1_name", "no"));

but if i try to access any key i just get the default text back. The text input is being saved in sharedPreferences because if i click on an editText the text i set proviously is there allready, its just the summary shows the default text. Thank you in advance for your help i am most likely missing something very simple.

xml for preferences screen

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory

    android:title="@string/prefs_relay_title">
    <EditTextPreference
        android:dialogTitle="Relay1"
        android:key="relay1_name"
        android:summary="Relay 1"
        android:title="Relay 1"
        android:defaultValue="Relay 1" />

</PreferenceCategory>
</PreferenceScreen>

action where fragment is created public class PrefsActivity extends Activity {

public static final String PREFS_NAME = "prefs";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();    // Load the prefs screen up 
 }
}

prefs fragment itself

public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

//public static final String PREFS_NAME = "prefs";

@Override
public void onCreate(Bundle savedInstanceState) {

    Log.v("PrefsFragment", "Creating prefs fragment");

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs_xml);

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

}

void loadDefaults() {

    SharedPreferences def = getActivity().getPreferences(0);
    Log.v("Something", def.getString("relay1_name", "no"));

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    if (key.equals("relay1_name")){
        Preference connectionPref = findPreference(key);
        connectionPref.setSummary(sharedPreferences.getString(key, ""));

}

@Override
public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}

}
Tim
  • 35,413
  • 11
  • 95
  • 121
James Chip
  • 61
  • 1
  • 4

1 Answers1

1

I think PreferenceScreen uses application default shared preferences, while your fragment is using getActivity().getPreferences()

Looking at getPreferences() in the Activity documentation, it does not retrieve the application shared preferences, but rather the preferences which are private to a particular activity. I think this is not the same as the ones stored by a PreferenceScreen.

I might suggest you replace

SharedPreferences def = getActivity().getPreferences(0);

with

SharedPreferences def = PreferenceManager.getDefaultSharedPreferences();

and see if that helps.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Thankyou for your answer, unfortunatly getSharedPreferences() did not word as it requires a context as an argument which a fragment does not have. However i did solve the problem using - SharedPreferences def = getPreferenceScreen().getSharedPreferences(); to access the sharedPreferences of the preference screen directly. – James Chip Oct 16 '12 at 08:09