所以这里的人是问题,我抬头看了看这个
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#commit%28%29
它提到“请注意,当两个编辑器同时修改首选项时,最后一个调用提交的将获胜。”
假设我的 Activity 是这样的:
SharedPreferences.Editor editor();
onCreate(....)
{
//start the instance of editor
......
if(condition1)
editor.put...
editor.commit()
}
onPause()
{
if(condition1)
{
editor.commit()
}
}
这行得通吗?因为在我的应用程序中,我必须在在线时存储用户凭据,并且很少向服务器提交回记录用户 ID 的服务器,而在离线时(即条件 1),它不是。 onPause 是我这样做的地方。所以任何人都可以确认这一点。谢谢。
**putBoolean() 似乎工作正常。这是一个巨大的代码 fragment ,所以我可能在用户凭据逻辑的某个地方犯了一个错误。所以,我只想确认 editor.commit() 的用法。*
最佳答案
如果您有类(class)成员 SharedPreferences.Editor 编辑器,那么是的,您可以在整个类(class)中毫无顾虑地使用它。另外,查看方法签名:
public abstract boolean commit()
您可以检查提交的结果以确保值已成功写入。
boolean result = editor.commit();
关于android - SharedPreferences.Editor.commit() 可以在同一 Activity/fragment 中多次使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415158/