SharedPreferences中的commit()和apply()有什么区别 [英] What's the difference between commit() and apply() in SharedPreferences
问题描述
我在我的Android应用中使用SharedPreferences
.我正在使用共享首选项中的commit()
和apply()
方法.当我使用AVD 2.3时,它没有显示任何错误,但是当我在AVD 2.1中运行代码时,apply()
方法显示了错误.
I am using SharedPreferences
in my android app. I am using both commit()
and apply()
method from shared preference. When I use AVD 2.3 it shows no error, but when I run the code in AVD 2.1, apply()
method shows error.
那么这两者之间有什么区别?通过仅使用commit()
,我可以毫无问题地存储首选项值吗?
So what's the difference between these two? And by using only commit()
can I store the preference value without any problem?
推荐答案
apply()
已在2.3中添加,它会提交而不会,返回表示成功或失败的布尔值.
apply()
was added in 2.3, it commits without returning a boolean indicating success or failure.
commit()
返回 true ,否则返回 false .
apply()
是因为Android开发团队注意到几乎没有人注意到返回值,因此apply更快,因为它是异步的.
apply()
was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous.
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()
这篇关于SharedPreferences中的commit()和apply()有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!