java - Android: SharedPreferences loses 2 of its variables after closing app -
i save couple variables via sharedpreferences without problem. however, 2 of these variables reset after restart app. think problem happens while saving, not while loading, because if change default value loading, doesnt use value, goes 0.
i call method in onpause:
public void savestats() { sharedpreferences pref = getsharedpreferences(shared_preferences, this.mode_private); sharedpreferences.editor editor = pref.edit(); editor.putlong(seconds_played_total_file, secondsplayedtotal); editor.putfloat(currency_gained_total_file, currencygainedtotal); editor.apply(); }
and load onresum:
sharedpreferences pref = getsharedpreferences(shared_preferences, this.mode_private); seconds_played_total = pref.getlong(seconds_played_total_file, 0); currency_gained_total = pref.getfloat(currency_gained_total_file, 0);
the variables public , static. save , load similar public static variables without problem, 2 ones save @ onpause().
any idea?
you try replacingeditor.apply();
editor.commit()
from android documentation:
unlike commit(), writes preferences out persistent storage synchronously, apply() commits changes in-memory sharedpreferences starts asynchronous commit disk , won't notified of failures.
link: https://developer.android.com/reference/android/content/sharedpreferences.editor.html#apply()
Comments
Post a Comment