getContext() 、 getApplicationContext() 、 getBaseContext() 和“this"之间的区别 [英] Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

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

问题描述

getContext()getApplicationContext()getBaseContext() 和 "this 之间有什么区别>"?

What is the difference between getContext() , getApplicationContext() , getBaseContext() , and "this"?

虽然这是一个简单的问题,但我无法理解它们之间的基本区别.如果可能,请举一些简单的例子.

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

推荐答案

  • View.getContext():返回视图当前运行的上下文.通常是当前活动的Activity.

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

      Activity.getApplicationContext():返回整个应用程序的上下文(所有Activity都在里面运行的进程)的).如果需要,请使用它而不是当前的活动上下文上下文与整个应用程序的生命周期相关,而不仅仅是当前活动.

      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.

      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() 和“this"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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