应用程序上下文的共享preferences? [英] Application context for SharedPreferences?

查看:169
本文介绍了应用程序上下文的共享preferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用的ApplicationContext 共享preferences 工作和启动,例如, RemoteService

Can I use ApplicationContext for working with SharedPreferences and starting, for example, RemoteService?

从逻辑上讲,我认为我可以。是否有这种做法的任何细微差别?

Logically, I think that I can. Are there any nuances in such an approach?

推荐答案

您应该使用活动服务 上下文,即'这个',除非你有一个清晰而有力的理由不这么做。仅使用的ApplicationContext 如果你显然需要一个参考应用程序的全局状态。

You should use the Activity or Service Context, i.e. 'this', unless you have a clear and strong reason not to. Only use ApplicationContext if you clearly need a reference to the global state of your application.

从上<一个Android开发者API文档href="http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29"><$c$c>ContextWrapper's getApplicationContext() 方式:

From Android Developers API docs on ContextWrapper's getApplicationContext() method:

这通常应该只有当你需要一个上下文的生命周期独立于当前的环境中使用,这是联系在一起的过程,而不是当前组件的生命周期。

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

使用的ApplicationContext(...),而不是如活动或服务的情况下],很容易导致严重的泄漏,如果你忘了注销,解除绑定等。

using the ApplicationContext (...) [as opposed to e.g. an Activity or Service context] can easily lead to serious leaks if you forget to unregister, unbind, etc.

例如,要检索共享preferences 活动来如更改显示给用户的数据,使用 this.getShared preferences(...),因为没有明确的理由,你为什么会需要挖掘到应用程序的生命周期。同样,在服务,使用 this.getShared preferences(...)。 (请注意,活动和服务的的上下文。他们间接地扩展 android.content.Context

For example, to retrieve SharedPreferences in an Activity to e.g. change data displayed to the user, use this.getSharedPreferences(...) since there is no clear reason why you would need to tap into the application's lifecycle. Same, in a Service, use this.getSharedPreferences(...). (Note that Activity and Service are Contexts. They indirectly extend android.content.Context)

CommonsWare写了深入的答案:<一href="http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context">When调用活动上下文或应用程序上下文?他做的情况下,调用getApplicationContext()几乎总是错的,以及何时使用它概括了少数例外

CommonsWare has written an in-depth answer: When to call activity context OR application context? where he makes the case that calling getApplicationContext() "is almost always wrong" and outlines the few exceptions when to use it:

  • 绑定到从活动服务。
  • 的东西需要被连接到具有全局范围的上下文。

CommonsWare还链接了一个<一个href="http://stackoverflow.com/questions/5228160/what-exactly-does-using-the-application-context-mean/5228494#5228494">answer通过Android框架工程师戴安娜Hackborn:

CommonsWare also links to an answer by Android Framework Engineer Dianne Hackborn:

第一条规则我会给你:如果你不知道为什么你需要[应用上下文],你也许并不需要它(...)你想用getApplicationContext()的唯一情况是,当你需要一个背景下,存在一个Activity类的生命周期(或其他组件)之外。

The first rule I would give you: if you don't know why you need [Application Context], you probably don't need it (...)The only time you want to use getApplicationContext() is when you need a Context that exists outside of the lifecycle of an Activity class (or other component).

更多的答案上就有关问题进行了讨论同一主题的ApplicationContext

More answers on the same subject with discussions on the issues relating to ApplicationContext:

  • <一个href="http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext">Android: ProgressDialog.show()崩溃与getApplicationContext
  • <一个href="http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context">Difference活动上下文和应用程序上下文之间
  • Android: ProgressDialog.show() crashes with getApplicationContext
  • Difference between Activity Context and Application Context  

这篇关于应用程序上下文的共享preferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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