I am changing and committing a SharedPreference in my SyncAdapter after successful sync, but I am not seeing the updated value when I access the preference in my Activity (rather I am seeing the old value). What am I doing wrong? Different Contexts?
My SyncAdapter where I update the preference:
class SyncAdapter extends AbstractThreadedSyncAdapter {
private int PARTICIPANT_ID;
private final Context mContext;
private final ContentResolver mContentResolver;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mContext = context;
mContentResolver = context.getContentResolver();
}
public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
mContext = context;
mContentResolver = context.getContentResolver();
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
PARTICIPANT_ID = Integer.parseInt(prefs.getString("participant_id", "0"));
if (success) {
// save and set the new participant id
PARTICIPANT_ID = newParticipantId;
prefs.edit().putString("participant_id", String.valueOf(newParticipantId)).commit();
}
}
}
The Service initializing the SyncAdapter with the ApplicationContext:
public class SyncService extends Service {
private static final Object sSyncAdapterLock = new Object();
private static SyncAdapter sSyncAdapter = null;
@Override
public void onCreate() {
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new SyncAdapter(getApplicationContext(), false);
}
}
}
@Override
public IBinder onBind(Intent intent) {
return sSyncAdapter.getSyncAdapterBinder();
}
}
A static function within the Application called by the Activity that checks the SharedPreference. This does not return the value committed in the SyncAdapter, but the old value. (My SettingsActivity and other Activities also use the old value.):
public static boolean isUserLoggedIn(Context ctx) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
int participantId = Integer.parseInt(prefs.getString("participant_id", "0"));
LOGD("dg_Utils", "isUserLoggedIn.participantId: " + participantId);// TODO
if (participantId <= 0) {
ctx.startActivity(new Intent(ctx, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
return false;
}
return true;
}
UPDATE: I am getting the new value when I completely close the app (swipe it from the apps running). I also have a SharedPreferenceChangeListener, which is not fired when the preference is updated.
private final SharedPreferences.OnSharedPreferenceChangeListener mParticipantIDPrefChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals("participant_id")) {
LOGI(TAG, "participant_id has changed, requesting to restart the loader.");
mRestartLoader = true;
}
}
};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// subscribe to the participant_id change lister
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
PARTICIPANT_ID = Integer.parseInt(prefs.getString("participant_id", "0"));
prefs.registerOnSharedPreferenceChangeListener(mParticipantIDPrefChangeListener);
}