1

I recently wrote a small app for tracking my workouts and the app crashed for some reason. When I opened it again all of my data, which had been stored in shared preferences, was lost. It's been working fine for about a month and only just now has it had this problem.

So under what circumstances might shared preferences get deleted? Is there a better option for saving data easily so that I can avoid this in the future?

1 Answers1

3

If you’re using commit() after changing value on Shared Preference method it will save quickly but if you using apply() method cannot be sure about saving because apply is asynchronous and it may not save the data.

        SharedPreferences.Editor editor =                  sharedpreferences.edit();
       editor.putString(Name, n);
        editor.putString(Phone, ph);
        editor.putString(Email, e);
        editor.commit();

Other important point is exactly when the apps crash before saving data or on saving data or after saving data.

shared preferences easily will erase by deleting application or clear data button in setting.

Nima Mohammadi
  • 353
  • 2
  • 9
  • Good guess! This makes perfect sense if the poster has lifecycle issues around calling commit. +1 on your answer. – JustinAngel Jan 18 '18 at 21:49