Thursday, 12 September 2013

Multiple object of shared preference deletes data

Multiple object of shared preference deletes data

I'm using shared preferences throughout my application. I'm having a
problem though. I think my data is being lost on background.
SharedPreferences preferences = getSharedPreferences(pref_data,
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.putString("driverPassword", driverPassword);
editor.putString("carrierId", carrierId);
editor.putString("CCTID", cctid);
editor.putString("shipment", entityShipment);
editor.putString("isAccepted", "");
editor.commit();
And somewhere in the app I create another object and edit just one portion
of the data
SharedPreferences preferences = getSharedPreferences(pref_data,
Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("isDriverLogin", "True");
editor.commit();
I'm doing this throughout my application. Editing just one or two data.
But at some point my app crashes or the expected behavior is not
happening. This behaviors are dependent on my preference data.
I tried fixing it by just creating one object of share preferences and
always edit them with all the data. For example I have 5 data on shared
preferences. If I just need to edit one, I will still put some data on the
other 4 just to make sure nothing is being overwritten.
Do you guys have any idea?

No comments:

Post a Comment