共享偏好:无法在单独的方法中更新已保存的值

时间:2016-04-23 23:29:23

标签: android

我正在编写一个游戏应用程序,我需要保存最高分并在下次用户播放时将其恢复。因此,当玩家死亡时发生保存,并且当游戏再次开始时发生检索(读取):两者都采用不同的方法。

保存:

SharedPreferences myPrefs = getContext().getSharedPreferences(HIGH, Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt(HIGHSCORE, best);
prefsEditor.commit();

阅读:

SharedPreferences myPrefs = getContext().getSharedPreferences(HIGH, Context.MODE_PRIVATE);
int highestScore = myPrefs.getInt(HIGHSCORE, 1);
best=highestScore;

正如您可以看到检索highestscore时的默认值是1.但我总是得到1!因此可能没有正确地进行保存。请帮忙。谢谢。

0 个答案:

没有答案