Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedPreferences overwrite other value

I have a problem with the SharedPreferences If I want to save two different values. I tried with this code:

SharedPreferences sharedPref = getSherlockActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

editor.putInt(getString(R.string.SavedStartSilentHour), hour);
editor.commit();

editor.putInt(getString(R.string.SavedStartSilentMinute), min);
editor.commit();

// One editor.commit() is enough

If I run this code the first value is overwritten with the seccond value. If I delte the second part the value is saved correctly. Why is that?

like image 811
Cilenco Avatar asked Oct 21 '22 07:10

Cilenco


1 Answers

Your code seems perfect!

You could simplify the thing by committing all the stuff after all the "puts" operations. Although I don't think that this could be your problem...

Just make sure SavedStartSilentHour and SavedStartSilentMinutes xml's defined values are correctly defined, i.e., if they are the same of course they will be overwritten. (This is the only one thing that makes any sense to me considering your code).

Let mew know of your progress ;)

like image 65
yugidroid Avatar answered Oct 24 '22 04:10

yugidroid