问题描述
已经有很多关于这两种上下文是什么的帖子......但我仍然没有完全正确
There has been a lot of posting about what these two contexts are.. But I'm still not getting it quite right
据我目前的理解:每个都是其类的一个实例,这意味着一些程序员建议您尽可能多地使用 this.getApplicationContext()
以免泄漏"任何内存.这是因为另一个 this
(获取 Activity
实例上下文)指向一个 Activity
,每次用户倾斜手机或离开应用程序等等.显然垃圾收集器 (GC) 没有捕获,因此使用了太多内存.
As I understand it so far:Each is an instance of its class which means that some programmers recommend you to use this.getApplicationContext()
as often as possible in order to not "leak" out any memory. This is because the other this
(getting the Activity
instance context) points to an Activity
that is being destroyed each time the user tilts the phone or leave the app etc.. Which apparently the Garbage Collector (GC) doesn't catch and therefore uses too much memory..
但是任何人都可以提出一些非常好的编码示例,其中使用 this
是正确的事情(获取当前 Activity
实例的上下文)和应用程序上下文将无用/错误?
But can anyone please come up with some really good coding examples where it would be the right thing to use this
(getting the context of the current Activity
instance) and the application context will be useless/wrong?
推荐答案
getApplicationContext()
几乎总是错误的.女士.Hackborn(以及其他人)非常明确地表示,当您知道为什么使用 时,仅使用
并且仅在您需要时使用 getApplicationContext()
>getApplicationContext()getApplicationContext()
.
getApplicationContext()
is almost always wrong. Ms. Hackborn (among others) have been very explicit that you only use getApplicationContext()
when you know why you are using getApplicationContext()
and only when you need to use getApplicationContext()
.
坦率地说,一些程序员"使用 getApplicationContext()
(或 getBaseContext()
,在较小程度上)是因为他们的 Java 经验有限.它们实现了一个内部类(例如,一个 OnClickListener
用于 Activity
中的 Button
)并且需要一个 Context
.不是使用 MyActivity.this
来获取外部类的 this
,而是使用 getApplicationContext()
或 getBaseContext()
获取一个 Context
对象.
To be blunt, "some programmers" use getApplicationContext()
(or getBaseContext()
, to a lesser extent) because their Java experience is limited. They implement an inner class (e.g., an OnClickListener
for a Button
in an Activity
) and need a Context
. Rather than using MyActivity.this
to get at the outer class' this
, they use getApplicationContext()
or getBaseContext()
to get a Context
object.
你只使用getApplicationContext()
,当你知道你需要一个Context
的东西可能会活得更久比您可以使用的任何其他可能的 Context
.场景包括:
You only use getApplicationContext()
when you know you need a Context
for something that may live longer than any other likely Context
you have at your disposal. Scenarios include:
使用
getApplicationContext()
如果您需要绑定到Context
的东西,它本身将具有全局范围.我使用getApplicationContext()
,例如,在WakefulIntentService
中,用于服务的静态WakeLock
.由于WakeLock
是静态的,我需要一个Context
来获取PowerManager
来创建它,所以使用getApplicationContext()
.
Use
getApplicationContext()
if you need something tied to aContext
that itself will have global scope. I usegetApplicationContext()
, for example, inWakefulIntentService
, for the staticWakeLock
to be used for the service. Since thatWakeLock
is static, and I need aContext
to get atPowerManager
to create it, it is safest to usegetApplicationContext()
.
当您从 Activity
绑定到 Service
时使用 getApplicationContext()
,如果您希望传递 通过
(即绑定的句柄).Android 通过这些 onRetainNonConfigurationInstance()
,Activity
实例之间的ServiceConnectionServiceConnections
在内部跟踪绑定,并保存对创建绑定的 Contexts
的引用.如果您从 Activity
绑定,那么新的 Activity
实例将具有对 ServiceConnection
的引用,它具有对旧 Activity
,旧的Activity
不能被垃圾回收.
Use getApplicationContext()
when you bind to a Service
from an Activity
, if you wish to pass the ServiceConnection
(i.e., the handle to the binding) between Activity
instances via onRetainNonConfigurationInstance()
. Android internally tracks bindings via these ServiceConnections
and holds references to the Contexts
that create the bindings. If you bind from the Activity
, then the new Activity
instance will have a reference to the ServiceConnection
which has an implicit reference to the old Activity
, and the old Activity
cannot be garbage collected.
一些开发人员将Application
的自定义子类用于他们自己的全局数据,他们通过getApplicationContext()
检索这些数据.那当然是可能的.我更喜欢静态数据成员,如果没有其他原因,你只能拥有 one 自定义 Application
对象.我使用自定义 Application
对象构建了一个应用程序,发现它很痛苦.女士.Hackborn 也同意这个立场.
Some developers use custom subclasses of Application
for their own global data, which they retrieve via getApplicationContext()
. That's certainly possible. I prefer static data members, if for no other reason than you can only have one custom Application
object. I built one app using a custom Application
object and found it to be painful. Ms. Hackborn also agrees with this position.
以下是为什么不无论走到哪里都不要使用 getApplicationContext()
的原因:
Here are reasons why not to use getApplicationContext()
wherever you go:
它不是一个完整的
Context
,它支持Activity
所做的一切.您尝试使用此Context
执行的各种操作都将失败,主要与 GUI 相关.
It's not a complete
Context
, supporting everything thatActivity
does. Various things you will try to do with thisContext
will fail, mostly related to the GUI.
如果来自 getApplicationContext()
的 Context
保留了由您对其调用而创建但您没有清理的内容,则它可能会造成内存泄漏.对于 Activity
,如果它持有某些东西,一旦 Activity
被垃圾收集,其他所有东西也会被清除.Application
对象在您的进程的生命周期内保持不变.
It can create memory leaks, if the Context
from getApplicationContext()
holds onto something created by your calls on it that you don't clean up. With an Activity
, if it holds onto something, once the Activity
gets garbage collected, everything else flushes out too. The Application
object remains for the lifetime of your process.
这篇关于何时调用活动上下文或应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!