当调用活动上下文或应用程序上下文? [英] When to call activity context OR application context?
问题描述
目前已经有很多张贴关于什么这两个背景是。但我仍然没有得到它完全正确
There has been a lot of posting about what these two contexts are.. But I'm still not getting it quite right
据我所知,到目前为止:
每一个都是它的类的实例,这意味着一些程序员建议你使用 this.getApplicationContext()
尽可能经常为了不漏出的内存。这是因为其他这
(获得活动
实例上下文)指向一个活动
正被每个使用者倾斜手机或离开应用程序等等。这显然垃圾收集器(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..
不过,任何人都可以,请拿出一些真正好的编码例子,这将是正确的选择使用这
(获得的电流活动
)实例和应用程序上下文将是无用/错了吗?
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()
几乎总是错的。 <一href="http://stackoverflow.com/questions/5228160/what-exactly-does-using-the-application-context-mean/5228494#5228494">Ms. 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
为按钮
在活动
),需要一个上下文
。而不是使用 MyActivity.this
以获得在外部类'这
,他们用 getApplicationContext ()
或 getBaseContext()
获得上下文
对象。
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()
当你的知道的你需要一个上下文
的东西可能更长寿比其他任何可能的上下文
在您的处置你。方案包括:
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()
如果你需要的东西绑在上下文
这本身也会有全局作用域。我用getApplicationContext()
,例如,WakefulIntentService
,对静态WakeLock
来用于服务。由于该WakeLock
是静态的,而我需要一个上下文
来获得在电源管理器
以创建它,它是最安全的方法是使用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()
.
使用 getApplicationContext()
当你绑定到服务
从活动
,如果你想通过 ServiceConnection
(即句柄绑定)活动的
通过 onRetainNonConfigurationInstance实例()
。机器人内部通过这些 ServiceConnections
跟踪绑定,并持有引用到上下文
创造的绑定。如果从活动结合
,那么新的活动
实例都会有一个参考 ServiceConnection
里面有一个隐含的引用老活动
,和老活动
不能垃圾收集。
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.
有些开发人员使用的应用程序
自定义子类为自己的全球数据,他们通过检索getApplicationContext()
。这当然是可能的。我preFER静态数据成员,如果没有别的原因,你只能拥有的一个的自定义应用程序
对象。我建立使用自定义应用程序
对象,一个应用程序,并发现它是痛苦的。 <一href="http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android/3827166#3827166">Ms. 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:
-
这不是一个完整的
上下文
,支持一切活动
一样。各种各样的事情,你会尝试做这个上下文
将失败,<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/7a648edddccf6f7d">mostly有关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()
保存到所创造的东西就可以了你的电话,你不清理。随着活动
,如果持有到的东西,一旦活动
被垃圾回收,一切冲出来了。该应用程序
的对象仍然是流程的生命周期。
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.
这篇关于当调用活动上下文或应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!