getDefaultSharedPreferences() 和 getPreferences() 之间有什么区别? [英] What's the difference between getDefaultSharedPreferences() and getPreferences()?

查看:27
本文介绍了getDefaultSharedPreferences() 和 getPreferences() 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习开发 Android 应用"Udacity 课程.在第 3 课:新活动和意图 > 使用 SharedPreferences"部分中,讲师让我在 Android 开发者网站上挖掘如何从 SharedPreferences 获取用户首选项.但是,我发现官方文档和课程的解决方案不同.

I'm currently taking the "Developing Android Apps" Udacity course. In the "Lesson 3: New Activities and Intents > Use SharedPreferences" segment, the instructor asked me to dig around the Android Developer site for how to get the user preferences from SharedPreferences. However, I found it different between the official documentation and the course's solution.

Udacity 课程的解决方案说,获取SharedPreferences PreferenceActivity 的实例,你应该调用:

The Udacity course's solution says, to grab a SharedPreferences instance for the PreferenceActivity, you should call:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

(其中 getActivity() 在这里只是 Context,因为它是在 Fragment 内部调用的.)

(where getActivity() is simply the Context here because it's called inside a Fragment.)

虽然 官方文档在 Android 开发者网站上表示您应该调用:

While the official documentation on Android Developer Site indicates that you should call:

SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE)

那么PreferenceManager.getDefaultSharedPreferences(Context context)Activity.getPreferences(int mode)有什么区别?

请注意:这个问题不涉及需要文件名的getSharedPreferences().这是关于 getPreferences()getDefaultSharedPreferences() 之间的区别.

Please note: This question does not involve anything about getSharedPreferences() which requires a file name. It's about the difference between getPreferences() and getDefaultSharedPreferences().

提前致谢.

推荐答案

根据您提供的 Android 文档链接

Acoording to the link you provided to Android documentation

getSharedPreferences() - 如果您需要通过名称标识多个首选项文件,请使用此选项,您可以使用第一个参数指定该名称.

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

getPreferences() - 如果您的 Activity 只需要一个首选项文件,请使用此选项.因为这将是您的活动的唯一首选项文件,所以您无需提供名称.

getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

因此,当您要保存/检索的数据可用于应用程序中的不同活动时,请使用 getSharedPreferences.如果这些首选项仅在一个活动中使用,您可以使用 getPreferences.

So it is, use getSharedPreferences when the data you want to save/retrieve can be used from different activities in the app. If those preferences will only be used in one Activity, you can use getPreferences.

还请注意,如您链接的帖子中所述,getDefaultSharedPreferences 将使用默认名称,如com.example.something_preferences",但 getSharedPreferences 将需要名称"

also note that as said in the post you linked 'getDefaultSharedPreferences will use a default name like "com.example.something_preferences", but getSharedPreferences will require a name'

这篇关于getDefaultSharedPreferences() 和 getPreferences() 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆