活动上下文和应用程序上下文之间的区别 [英] Difference between Activity Context and Application Context

查看:194
本文介绍了活动上下文和应用程序上下文之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有我难倒,我是在Android 2.1系统,R8 SDK使用这样的:

This has me stumped, I was using this in Android 2.1-r8 SDK:

ProgressDialog.show(getApplicationContext(), ....);

和也

Toast t = Toast.makeText(getApplicationContext(),....);

使用 getApplicationContext()崩溃两者 ProgressDialog 吐司 ...。这导致我对这个问题的:

using getApplicationContext() crashes both ProgressDialog and Toast .... which lead me to this question:

什么是活动内容和应用程序上下文之间的实际差异,尽管共享的措辞语境?

What is the actual differences between a activity context and application context, despite sharing the wording 'Context'?

推荐答案

他们上下文< /一>,但应用程序实例绑定到应用程序的生命周期,而活动实例绑定到活动的整个生命周期。因此,他们有机会获得对应用环境的不同信息。

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

如果你阅读文档的<一个href="http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29">getApplicationContext它指出,你应该只使用这个,如果你需要一个环境的生命周期独立于当前上下文。这不适用于在任一的实施例。

If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context. This doesn't apply in either of your examples.

的活度上下文presumably具有关于当前的活动,是要完成这些呼叫的一些信息。如果你表现出确切的错误信息,也许能够指向究竟需要。

The Activity context presumably has some information about the current activity that is necessary to complete those calls. If you show the exact error message, might be able to point to what exactly it needs.

但在一般情况下,使用活动的背景下,除非你有一个很好的理由。

But in general, use the activity context unless you have a good reason not to.

这篇关于活动上下文和应用程序上下文之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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