gpt4 book ai didi

android - 清除首选项并使其使用 XML 文件中的值的正确方法?

转载 作者:太空狗 更新时间:2023-10-29 15:29:50 25 4
gpt4 key购买 nike

所以我正在做一些开发并更改了我在 settings.xml 文件中的一些首选项的键名 (android:key)。在一些测试中,我意识到旧设置(旧键和值)仍然存在,即使我的新 settings.xml 文件不再有它们。

当我的应用程序启动时,我一直在这样做:

 PreferenceManager.setDefaultValues(this, R.xml.settings, false);

这不会删除我的设置文件中不再存在的旧设置。

然后尝试这个:

PreferenceManager.setDefaultValues(this, R.xml.settings, true);

最后是这个:

  PreferenceManager.getDefaultSharedPreferences(this).edit().clear();
PreferenceManager.setDefaultValues(this, R.xml.settings, true);

但是旧的键/值仍然存在。好的,所以现在我只是重新启动了模拟器并选中了“清除用户数据”,这样我肯定会成功的。但除此之外,正确的做法是什么?

清除旧设置的正确方法是什么?

最佳答案

在你的最后一个例子之后,你必须做一个提交。我建议将这些步骤拆分开来使其更易于阅读。

SharedPreferences sharedPrefs = getDefaultSharedPreferences(this);
Editor editor = sharedPrefs.edit();
editor.clear();
editor.commit();

关于android - 清除首选项并使其使用 XML 文件中的值的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273761/

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