I'm using context.getSharedPreference instead of getDefaultSharedPreferences. Means :
SharedPreferences checkboxSetting = context.getSharedPreferences(
"myPreferenceDB", Context.MODE_PRIVATE);
boolean flag = checkboxSetting.getBoolean("checkboxKey",true);
And preference.xml :
<CheckBoxPreference
android:key="checkBoxPrefff"
android:title="@string/title"
android:defaultValue="true"/>
Application setting has a checkboxpreference that I want to get the value of, checked or not checked.
Will this work?
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equalsIgnoreCase("checkBoxPrefff")) {
sharedPreferences.getBoolean(key,true);
}
}
Does sharedPreferences use default database ( ..._preference.xml ) or my defined database ( "myPreferenceDB" )? Does key is "checkboxKey" or is null?
Because when I want to get value like
SharedPreferences temp = context.getSharedPreferences(
"myPreferenceDB", Context.MODE_PRIVATE);
boolean flag = temp.getBoolean("checkboxKey",true);
it's wrong and gets back defValue (true) . But when use like
SharedPreferences temp = PreferenceManager.getDefaultSharedPreferences(context);
boolean flag = temp.getBoolean("checkboxKey",true);
it works.