gpt4 book ai didi

android - 没有相应的 commit() 或 apply() 调用的 SharedPreferences.edit()

转载 作者:行者123 更新时间:2023-11-29 14:19:39 26 4
gpt4 key购买 nike

我正在为我的应用程序的 Intro Slider 使用 SharedPreferences。但是,我在这一行遇到错误:

class PrefManager {
private SharedPreferences pref;
private SharedPreferences.Editor editor;


private static final String PREF_NAME = "welcome";

private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";

PrefManager(Context context) {
int PRIVATE_MODE = 0;
pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}

void setFirstTimeLaunch(boolean isFirstTime) {
editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
editor.commit();
}

boolean isFirstTimeLaunch() {
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}

}

editor = pref.edit();

如果我在调用 edit() 后不调用 commit() 或 apply() 会发生什么情况?

最佳答案

如果您不调用 commit() 或 apply(),您的更改将不会被保存。

  • Commit() 将更改同步并直接写入文件
  • Apply() 将更改写入内存中的 SharedPreferences立即但开始异步提交到磁盘

关于android - 没有相应的 commit() 或 apply() 调用的 SharedPreferences.edit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com