I'm having troubles trying to get SharedPreferences working.
this is the code:
/**
* Sets the software in synchronizing status.
* @param synchronizing Boolean
*/
public void setSynchronizing(boolean synchronizing) {
if(D) Log.d(TAG, "Called: setSynchronizing("+synchronizing+")");
SharedPreferences preferences = mContext.getSharedPreferences(SharedPrefsConstants.PREFERENCES, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(SharedPrefsConstants.SYNCHRONIZING, synchronizing);
boolean result = editor.commit();
if(!result)
Log.w(TAG, "Cannot store the preference.");
if(!synchronizing)
BroadcastUtils.stopSynchronizing(mContext);
}
/**
* Returns whether the software is synchronizing.
* @return True if synchronization is happening.
*/
public boolean isSynchronizing() {
SharedPreferences preferences = mContext.getSharedPreferences(SharedPrefsConstants.PREFERENCES, 0);
boolean synchronizing = preferences.getBoolean(SharedPrefsConstants.SYNCHRONIZING, false);
if(D) Log.d(TAG, "Called: isSynchronizing Returning: "+synchronizing);
return synchronizing;
}
and this is the logcat output, please note that i'm using two separate processes in my application, i'll call them app and app:bg:
**app** D/StorageManager﹕ Called: setSynchronizing(true)
**app** D/StorageManager﹕ Called: setSynchronizing(true)
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true
**app:bg** D/StorageManager﹕ Called: setSynchronizing(false)
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true
StorageManager is a singleton instance, but there are two of those instances, one for each process.
Even if setSynchronizing(false) is being called from background thread, the physical preference file is changed correctly, but in the foreground thread it is still true.
You can see that isSynchronizing method is returning true after setSynchronizing sets that variable to false. Question is: why do this happens? This is the first time i use SharedPreferences in this software, so it cannot be set anywhere else.
This is the preference file stored inside the phone, when isSynchronizing is still returning TRUE:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="synchronizing" value="false" />
</map>
The only thing that i can think of is that SharedPreferences holds some sort of cache in memory, if you can confirm this there is some way to force the update of a SharedPreference?
I must also say that a considerable amount of time is passing between the variable being set to false and any other subsequent call to isSynchronizing from the foreground thread.