I am trying to use shared preferences to store my apps settings, but even though i tried to copy tutorial best as i can i still can’t make it work. Here are parts of my class:
public class ActivitySettings extends PreferenceActivity {
@SuppressWarnings("unused")
private static String TAG = "ActivitySettings";
private static final String PREFS_NAME = "preferences";
private static final String DISABLE_CHECK = "disableCheck";
private static final String ALWAYS_CONFIRM = "alwaysConfirm";
private static final String NEVER_CONFIRM = "neverConfirm";
private static final String SHOW_NOTIFICATION = "showNotification";
private static final String SHOW_ON_BOOT = "showOnBoot";
private static final String HIDE_ICON = "hideIcon";
private static final String LOGGING = "logging";
private Context context = this;
private CheckBoxPreference disableCodeCheck;
private CheckBoxPreference alwaysAskForConf;
private CheckBoxPreference neverAskForConf;
private CheckBoxPreference showNotif;
private CheckBoxPreference showAtBoot;
private CheckBoxPreference hideIcon;
private Preference exportData;
private Preference importData;
private Preference cleanUp;
private Preference reset;
private CheckBoxPreference logging;
private boolean isDisableCodeCheck;
private boolean isAlwaysAskForConf;
private boolean isNeverAskForConf;
private boolean isShowNotif;
private boolean isShowAtBoot;
private boolean isHideIcon;
private boolean isLogging;
private void loadPreferences() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
isDisableCodeCheck = prefs.getBoolean(DISABLE_CHECK, false);
isAlwaysAskForConf = prefs.getBoolean(ALWAYS_CONFIRM, false);
isNeverAskForConf = prefs.getBoolean(NEVER_CONFIRM, false);
isShowNotif = prefs.getBoolean(SHOW_NOTIFICATION, false);
isShowAtBoot = prefs.getBoolean(SHOW_ON_BOOT, false);
isHideIcon = prefs.getBoolean(HIDE_ICON, false);
isLogging = prefs.getBoolean(LOGGING, false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
disableCodeCheck = (CheckBoxPreference) findPreference("disable_code_check");
alwaysAskForConf = (CheckBoxPreference) findPreference("always_ask_for_conf");
neverAskForConf = (CheckBoxPreference) findPreference("never_ask_for_conf");
showNotif = (CheckBoxPreference) findPreference("show_notif");
showAtBoot = (CheckBoxPreference) findPreference("show_at_boot");
hideIcon = (CheckBoxPreference) findPreference("hide_icon");
exportData = findPreference("exp");
importData = findPreference("imp");
cleanUp = findPreference("clean_up");
reset = findPreference("reset");
logging = (CheckBoxPreference) findPreference("logging");
loadPreferences();
//here are also booleans modified
@Override
protected void onStop() {
super.onStop();
savePreferences();
}
private void savePreferences() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
prefs.edit().putBoolean(DISABLE_CHECK, isDisableCodeCheck);
prefs.edit().putBoolean(ALWAYS_CONFIRM, isAlwaysAskForConf);
prefs.edit().putBoolean(NEVER_CONFIRM, isNeverAskForConf);
prefs.edit().putBoolean(SHOW_NOTIFICATION, isShowNotif);
prefs.edit().putBoolean(SHOW_ON_BOOT, isShowAtBoot);
prefs.edit().putBoolean(HIDE_ICON, isHideIcon);
prefs.edit().putBoolean(LOGGING, isLogging);
prefs.edit().commit();
}
}
Here is what happens:
-
there are two files created in shared_prefs: preferences.xml and packagename_preferences.xml. Don’t know why, PREFS_NAME is provided.
-
loading settings is done in onCreate methos, saving and commiting in onStop
-
using adb shell and cat i’m looking into files while app is running and here is the scenario:
- i open settings and tick sth and packagename_preferences.xml is created with correct value
- i press back and second file is created – preferences.xml which looks like this:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map /> - when i open settings again everything looks like on first run and values in packagename_preferences.xml are changed to default(?)
-
i know there is and built in mechanism for PreferenceActivity but i don’t want to use it because I need access to other settings from other activities.
EDIT
I have created such app:
package pl.test;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class TestActivity extends Activity {
String s = "0";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences sp = getSharedPreferences("pref", 0);
s = sp.getString("setting2", "1");
s = "5";
}
@Override
public void onStop() {
super.onStop();
SharedPreferences sp = getSharedPreferences("pref", 0);
sp.edit().putString("setting2", s);
sp.edit().commit();
}
}
And it doesn’t work it just does not save string to pref.xml. What is wrong!?
Your problem is the fact that you are generating a new Editor object with every call to sp.edit(). So your call sp.edit().commit() is creating a new editor that has no changes to commit. Try this:
Alternatively, The editor methods are designed to be chained, so this would also work:
You have the same problem in your test code, which can be fixed like so: