获取 Android Context 的各种方法有什么区别? [英] What's the difference between the various methods to get an Android Context?

查看:32
本文介绍了获取 Android Context 的各种方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我见过的各种 Android 代码中:

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();
    }
 }

但是,我找不到任何合适的解释,说明哪种方法更可取,以及在什么情况下应该使用哪种方法.

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.

推荐答案

我同意 Android 中上下文的文档很少,但您可以从各种来源拼凑出一些事实.

I agree that documentation is sparse when it comes to Contexts in Android, but you can piece together a few facts from various sources.

这篇博文在官方 Google 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:

在常规的 Android 应用程序中,您通常有两种Context,活动和应用.

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

阅读这篇文章,进一步了解两者之间的区别,以及何时可能需要考虑使用应用程序上下文 (Activity.getApplicationContext()) 而不是使用 Activity 上下文 this).基本上,应用程序上下文与应用程序相关联,并且在应用程序的整个生命周期中始终相同,因为 Activity 上下文与 Activity 相关联,并且可能会因 Activity 在屏幕方向更改期间被销毁而多次销毁,并且

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.

除了 Dianne Hackborn 的帖子之外,我找不到关于何时使用 getBaseContext() 的任何信息,Dianne Hackborn 是一名从事 Android SDK 的 Google 工程师:

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.

这是来自 android-developers 新闻组,您可能也想考虑在那里提出您的问题,因为少数从事 Android 工作的人会实际监控该新闻组并回答问题.

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 Context 的各种方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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