转自:http://stackoverflow.com/questions/1026973/android-whats-the-difference-between-the-various-methods-to-get-a-context
In various bits of Android code I've seen:
在大量的android代码中,我看到如下的写法:
public class MyActivity extends Activity {
public void method() {
mContext = this; // since Activity extends Context
mContext = getApplicationContext();
mContext = getBaseContext();
}
}
However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.
不过,我不能找到任何像样的解释:那种写法是最好的,在哪种情况应使用哪种写法。
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
如果你有关于这方面的文章的地址,和关于如果选择错误的写法会产生的错误的指导,将不胜感激。
I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.
我承认,涉及android中Contexts的文章凤毛麟角,但是,我们还是可以从各种来源中拼凑出东西的。
This blog post on the official Google Android developers blog was written mostly to help address memory leaks, but provides some good information about contexts as well:
这个谷歌官方的Android开发者写,主要是为了帮助解决内存泄漏,的博客文章,同时也给contexts提供了一些良好的信息:
In a regular Android application, you usually have two kinds of Context, Activity and Application.
在一个普通的Android应用程序中,你通常有两种Context,Activity和Application。
Reading the article a little bit further tells about the difference between to the two and why you might want to consider using the application Context (Activity.getApplicaitonContext()) rather than using the Activity context ("this"). Basically the Application context is associated with the Applicaiton and will always be the same throughout the life cycle of you app, where as the Activity context is associated with the activity and could possible be destroyed many times as the activity is destroyed during screen orientation changes and such.
如果再深入读下这篇文章,它告诉了我们这两种Context的不同,并且为什么你会考虑使用应用程序Context(Activity.getApplicaitonContext())而不是使用活动Context(即"this")。 基本上来说呢,应用程序Context是和应用程序关联的,并且在程序的生命周期内,总是一样的。但是,活动Context是关联在某个活动上的,随着Activity的销毁(比如横竖屏切换等情况),这个Context也会被销毁N多次。
I couldn't find really anything about when to use getBaseContext() other than / from one /(a for a) liner from Dianne Hackborn, one of the Google engineers working on the Android SDK:
我真的没有找到任何关于使用 getBaseContext()的东西,除了Dianne Hackborn(一位编写Android SDK的谷歌工程师)的文章中的一句话:
Don't use getBaseContext(), just use the Context you have.
不要使用getBaseContext(),只使用你有的Context。
That was from a post on the android-developers newsgroup, you may want to consider asking your question there as well because a handful of the people working on Android actual monitor that newsgroup and answer questions.
那是来自 android-developers 新闻组的一篇邮件。你也可以考虑在那里问你的问题,因为确实有一大帮搞Android的人在管理这个小组,并且回答问题。
So overall it seems preferable to use the global application context when possible.
所以,总体来说,似乎最好在可能的情况下使用全局应用程序Context。
I got bad window token errors when using application context to Progress Dialog. So i used activity context instead.
我使用应用程序Context给进度条对话框的时候,出现了“window token errors”的错误。所以,我使用了活动Context,而不是应用程序Context。
PS:
Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.
如果你有关于这方面的文章的地址,和关于如果选择错误的写法会产生的错误的指导,将不胜感激。
来源:oschina
链接:https://my.oschina.net/u/195456/blog/79199