what's the difference between the various methods to get a Context?

来源:互联网 发布:aws s3 java 开发文档 编辑:程序博客网 时间:2024/05/29 09:04

今天来翻译一篇论坛问答,关于获取Context几种方法之间的区别。

问题:

In various bits of Android code I've seen:

   1: public class MyActivity extends Activity {
   2:     public void method() {
   3:        mContext = this;    // since Activity extends Context
   4:        mContext = getApplicationContext();
   5:        mContext = getBaseContext();
   6:     }
   7:  }

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.

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:

In a regular Android application, you usually have two kinds of Context, Activity and Application.

Reading the article a little bit further tells about the difference between the two and when you might want to consider using the application Context (Activity.getApplicationContext()) rather than using the Activity contextthis). Basically the Application context is associated with the Application and will always be the same throughout the life cycle of your app, where as the Activity context is associated with the activity and could possibly be destroyed many times as the activity is destroyed during screen orientation changes and such.

I couldn't find really anything about when to use getBaseContext() other than a post from Dianne Hackborn, one of the Google engineers working on the Android SDK:

Don't use getBaseContext(), just use the Context you have.

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.

So overall it seems preferable to use the global application context when possible.(转到文章出处)

问题:

在很多的android代码中,我看到有如下的写法:

   1: public class MyActivity extends Activity { 
   2:     public void method() { 
   3:        mContext = this;    // since Activity extends Context 
   4:        mContext = getApplicationContext(); 
   5:        mContext = getBaseContext(); 
   6:     } 
   7: }
 
不过,至于那种写法最好?在哪种情况应使用哪种写法?我还没有找到像样的解释。
如果你有关于这方面的文章的地址,以及如果选择错误的话会产生什么后果,本人将不胜感激。
回答:
我承认,涉及android中Contexts的文章很少,但是,我们还是可以从不同的地方拼凑出一些信息。
这个谷歌官方Android开发写的博客文章,主要是为了帮助解决内存泄漏,但同时也给contexts提供了一些有用的信息:
在一个普通的Android应用程序中,你通常有两种Context,即 Activity 和 Application。
如果再深入的读一下这篇文章,它告诉了我们这两种Context的不同,以及什么时候你会考虑使用 Application Context,也就是Activity.getApplicaitonContext(),而不是使用 Activity context,也就是 this 关键字。基本上来说,Application Context 是和应用程序相关联的,并且和应用程序具有相同的生命周期。而 Activity context 是关联在某个 Activity 上的,随着 Activity 的销毁这个Context 也会被销毁多次,比如横竖屏切换等情况。
至于 getBaseContext() ,我没有找到什么时候使用的信息,除了Dianne Hackborn(一位编写Android SDK的谷歌工程师)的文章中的一句话:
不要使用 getBaseContext(),只使用你有的 Context。
这是来自 android-developers 新闻组的一封邮件。你也可以考虑在那里问你的问题,因为确实有一群搞Android的人在管理这个小组,并且回答问题。
所以,总体来说,似乎最好在可能的情况下使用全局应用程序 Context。
附:这篇相关文章也是很不错的
0 0
原创粉丝点击