Preferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
boolean value = preferences.getBoolean("autoUpdate", true);
this works for me.
Domanda
I'm implementing preferences in my application and I've never worked with those before. I've read the documentation but I can't find a solution anywhere, I'm probably missing it though.
I've implemented preferences like this:
<CheckBoxPreference
android:title="@string/preference_update_urnik_title"
android:summary="@string/preference_update_urnik_summary"
android:key="autoUpdate"
android:defaultValue="true"/>
and I basically haven't done anything else. I'm now trying to get the value of this preference like this:
boolean autoUpdate = prefs.getBoolean("autoUpdate", true);
And no matter if the checkbox is checked or not, it will always return true, so I'm guessing the preferences are failing to write to SharedPreferences when I check/uncheck the checkbox. Do I have to do it manually? Because I thought it was supposed to happen automatically.
I left the preference class file empty:
public class NastavitveActivity extends PreferenceActivity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Soluzione
Preferences prefs = PreferenceManager.getDefaultSharedPreferences(context)
boolean value = preferences.getBoolean("autoUpdate", true);
this works for me.