I have alarm clocks. I create a new alarm clock, save time in Set in SharedPreferences.

buttons.add(dateFromTimePicker); sPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = sPref.edit(); ed.putStringSet(SAVE_DATE_CLOCK, buttons); ed.commit(); //перезагружаю активити Intent i = new Intent( this, this.getClass() ); finish(); this.startActivity(i); 

Then I restart the activation . It turns out that the activation opens and restores the Set from SharedPreferences, say, 3 alarms. Set stores 3 dates. All OK.

 //перед открытием заполним активити кнопками sPref = getPreferences(MODE_PRIVATE); buttons = sPref.getStringSet(SAVE_DATE_CLOCK, new HashSet<String>()); // Вот если я процесс работы не убил, а из работающего апк перезагружаю, //то buttons.size будет = 3 или 4 и тд, а если убью процесс. //Открываю апк и buttons.size = 1 

I kill the process of the application, again I open the application and op, the size of the set which I restore for some reason is not 3, but 1. One alarm clock.

Why if I kill the application process, let's say I reboot the device, then the number of elements in the set is reduced to 1?

  • show the program code - elik

1 answer 1

Most likely there is a rewrite in SP -sah. The problem in. onCreate or onStop onDestroy. SharedPreferences.Editor uses the Builder template to create a commit transaction. editor.putString (K, V) .putLong (k2, n) .commit (); After commit () you must use the new editor or call editor.clear () before reusing