什么是各种方法之间的差异,以获得上下文? [英] What's the difference between the various methods to get a Context?

查看:211
本文介绍了什么是各种方法之间的差异,以获得上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android的code各个位我见过的:

In various bits of Android code I've seen:

 public class MyActivity extends Activity {
    public void method() {
       mContext = this;    // since Activity extends Context
       mContext = getApplicationContext();
       mContext = getBaseContext();
    }
 }

不过,我无法找到任何像样的解释,这是preferable,以及在哪些应该用什么情况。

However I can't find any decent explanation of which is preferable, and under what circumstances which should be used.

指针,以什么可能打破,如果错了被选择的文件在此,与指导,会大大AP preciated。

Pointers to documentation on this, and guidance about what might break if the wrong one is chosen, would be much appreciated.

推荐答案

我同意的文件是稀疏的,当涉及到上下文中的Andr​​oid,但你可以从各种来源拼凑几个事实。

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开发者博客中写的大多是帮助解决内存泄漏,但提供了有关环境的一些好的信息,以及:

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:

在一个普通的Andr​​oid应用程序,   通常有两种语境,   行为和应用。

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

阅读文章一点点进一步讲述了两个,当你可能要考虑使用应用程序上下文( Activity.getApplicationContext()),而不是之间的差异使用活动上下文)。基本上,应用程序上下文与应用程序相关联,并且将永远是在你的应用程序,其中作为活动背景与活动有关,并可能被摧毁了许多倍,活动期间,屏幕方向的变化破坏和生命周期相同这样的。

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 context this). 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.

我无法找到真正有关何时使用getBaseContext()以外,从戴安娜Hackborn,在谷歌的工程师工作在Android SDK中的一个职位什么:

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:

不要使用getBaseContext(),只需使用   上下文你。

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

这是从对<一后href="http://groups.google.com/group/android-developers/browse_thread/thread/dbe5f18d3dba9aa9/fa4b981f635f16db?lnk=gst&q=getbaseContext#fa4b981f635f16db">android-developers新闻组,你可能要考虑问你的问题有作为,因为极少数人的工作该新闻组和回答问题的Andr​​oid实际监视器上。

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.

所以,总体看来preferable时,可以使用全局应用程序上下文。

So overall it seems preferable to use the global application context when possible.

这篇关于什么是各种方法之间的差异,以获得上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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