android - getContext() 、 getApplicationContext() 、 getBaseContext() 和 "this"之间的区别

标签 android this android-context

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

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

最佳答案

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

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

  • ContextWrapper.getBaseContext() :如果您需要从另一个上下文中访问上下文,则使用 ContextWrapper。这 ContextWrapper 通过内部引用的上下文 getBaseContext()。

关于android - getContext() 、 getApplicationContext() 、 getBaseContext() 和 "this"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10641144/

相关文章:

java - 通过原始音频重新创建带有和声的吉他弦

Java继承和this关键字

Java - 转换 "this[int x, int y]"

java - 蓝牙聊天示例和上下文参数

android - Android Application Context 会被破坏吗?

java - 如何从 firebase 读取子数据

android - 如何重启或关闭 Genymotion 设备?

Android - 如何将上下文参数传递给方法?

android - "Selection cannot be launched and there are no recent launches” Eclipse for Android Project Dev 时

java - 是否可以将新对象分配给 'this' ?