I wanted to make a option class, where you could load all preferences of a user. If there is no keys in the sharedPreference then I would like to load the xml defaults in Preference.
The problem is, that I can't get a class working with also an PreferenceActivity with implements OnSharedPreferenceChangeListener. And I can't get those keys loaded. So how do other applications do this? The commeted code with PreferenceManager did I also tried, but no luck there :(
Some code for the class options
static SharedPreferences GetSharedPreferences(Context context){
return context.getSharedPreferences("Name", Context.MODE_PRIVATE);
}
public static void ClearSharedPreferences(Context context){
GetSharedPreferences(context).edit().clear().apply();
}
public static void SaveTextSize(Context context, int size){
SharedPreferences sharedPreferences = GetSharedPreferences(context);
if (textSizeKey == null)
textSizeKey = context.getString(R.string.textSizeKey);
sharedPreferences.edit().putInt(textSizeKey, size).commit();
Log.v("Saving", "Text size save: " + size);
}
public static int LoadTextSize(Context context) {
SharedPreferences sharedPreferences = GetSharedPreferences(context);
if (textSizeKey == null)
textSizeKey = context.getString(R.string.textSizeKey);
if (!sharedPreferences.contains(textSizeKey))
Log.i("Load", "Key: " + textSizeKey + " not found");
return sharedPreferences.getInt(textSizeKey, 16);
}
Code for the Preference activity
public class SettingsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
private CheckBoxPreference mCheckBoxPreference;
private ListPreference mListPreference;
private static String updateCheckBoxName;
private static String updateListName;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
updateCheckBoxName = getString(R.string.options_main);
updateListName = getString(R.string.options_toggle);
//PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
//sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences = Options.GetSharedPreferences(this);
mCheckBoxPreference = (CheckBoxPreference) findPreference(updateCheckBoxName);
mListPreference = (ListPreference) findPreference(updateListName);
}
@Override
protected void onResume() {
super.onResume();
updateScreen();
// Set up a listener whenever a key changes
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
private void updateScreen() {
mCheckBoxPreference.setSummary(sharedPreferences.getBoolean(
updateCheckBoxName, false) ? "Will automatic update"
: "No automatic update");
mListPreference.setSummary(mListPreference.getEntry());
}
@Override
protected void onPause() {
super.onPause();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
// Hack for dialog
if (preference.equals(getString(R.string.textSizeKey))){
TextSizeAlert textSizeAlert = new TextSizeAlert(SettingsActivity.this) {
@Override
public void SizeCallback(int size) {
Options.SaveTextSize(getApplicationContext(), size);
}
};
textSizeAlert.ShowTextDialog();
return true;
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
updateScreen();
}
}
Main Activity:
public void SomeMethod() {
int updateTime = Options.LoadUpdateTime(this);
//do something with it
}
Got it figuered out, I had to use PreferenceManager.
So loading preference manager in the options
static SharedPreferences GetSharedPreferences(Context context){
//set xml default values if not set.
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
return PreferenceManager.getDefaultSharedPreferences(context);
}
Also, I had to wire the sharedPreferences register and unregister to the above method.