的getContext(),getApplicationContext(),getBaseContext()和QUOT之间的区别,这与QUOT; [英] Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

查看:276
本文介绍了的getContext(),getApplicationContext(),getBaseContext()和QUOT之间的区别,这与QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的getContext() getApplicationContext() getBaseContext之间的区别是什么)

虽然这是简单的问题,我无法理解他们之间的根本区别。请给可能的话一些简单的例子。

Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible.

推荐答案

  • <一个href="http://developer.android.com/reference/android/view/View.html#getContext%28%29">View.getContext():认为目前运行在返回的背景下,一般当前活动的活动。

  • View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.

    <一个href="http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29">Activity.getApplicationContext():返回整个应用程序上下文(所有的活动都内运行的过程 的)。如果你需要使用,而不是当前的活动背景下,这个 上下文绑在整个应用的生命周期中,不只是 当前活动。

    Activity.getApplicationContext(): Returns the context for the entire application (the process all the Activities are running inside of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.

    <一个href="http://developer.android.com/reference/android/content/ContextWrapper.html#getBaseContext%28%29">ContextWrapper.getBaseContext():如果您需要访问上下文从另一个方面,你使用ContextWrapper。该 上下文ContextWrapper里面提到的是通过访问 getBaseContext()。

    ContextWrapper.getBaseContext(): If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().

    这篇关于的getContext(),getApplicationContext(),getBaseContext()和QUOT之间的区别,这与QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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