I am using the Eclipse 3.5.2 IDE for Android development. I am using the following code to write to the default sharedpreferences file and I am running it in the adb emulator:
private SharedPreferences getOurSharedPreferences(Activity act) {
// return getSharedPreferences(SHARED_PREFS_FILENAME, MODE_PRIVATE);
return act.getPreferences(MODE_PRIVATE);
}
private void putStringToStorage(Activity act, String keyName, String s) {
// Store it.
SharedPreferences sharedPrefs = getOurSharedPreferences(act);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.remove(keyName);
// editor.putString(keyName, s);
editor.putString(keyName, "test123");
boolean bCommitted = editor.commit();
if (!bCommitted)
throw new RuntimeException("(AndroidApplication) Unable to save new string.");
// Get it back as a test.
// String s2 = getStringFromStorage(keyName);
}
The above method belongs to an Application sub-class of mine, that is why the method carries an Activity object parameter so I can have access to Activity.getPreferences(). Note, previously I was using the global getSharedPreferences() call but I changed to getPreferences() out of desperation due to the problems I am having. The reason I am writing "test123" instead of the variable "s" is to make sure the problem I'm having is not related to some value of "s" that perhaps the shared preferences file might not like.
Whenever I run the code editor.commit() always comes back FALSE. Some history. A while back I was able to write to the shared preferences file using a shared preferences file named "app_global", created by using the global getSharedPreferences() call instead of Activity.getPreferences(). I was able to write and read value from that shared preferences file, however, whenever I re-launched my app the values I wrote during the last session were gone. As part of my debugging efforts I went into the adb shell and deleted "app_global" from my app's shared_prefs folder. Ever since then the emulator appears unwilling to create a new shared preferences file, default one or otherwise.
Note, the LogCat window is always empty and the Console window does not show any errors, including during the launch of my application in the emulator. I also tried launching the application in Debug mode with "Wipe User Data" checked in the Debug configuration. That did nothing to help either. No matter what I do editor.commit() fails and the shared_prefs folder for my application when doing an "ls -a" from the adb shell is always empty.
My questions are:
I read in passing somewhere about "rebuilding the user image" for the emulator. Is that a technique I could use to try to fix this cluster of problems I am having with shared preference storage? If so, how do I do that?
Is there anything else I could try to fix this problem?
-- roschler