为什么 Android 使用多个上下文并且每个上下文都不同?
Why does Android use multiple contexts and every one is different?
我知道 android 中有很多这样的上下文
- 申请
- Activity
- 服务
- 内容提供者
- 广播接收器
所以当我创建一个 new TextView
时,我必须像这样在构造函数中传递 context
TextView textView = new TextView(this);
我知道它是必需的,但为什么不直接创建并 android 为我处理上下文?
我认为这与 Context
的生命周期有关。例如,Activity
可以终止并进行垃圾收集,这将使它的 Context
为空。这也可以反过来工作。在某些情况下,引用 Activity
的上下文会导致内存泄漏,并且 Activity
永远不会被垃圾回收。
也看看 this answer
上下文是记忆的方式 management.It 可能是 Android 应用程序中使用最多的元素……它也可能是最常被滥用的元素。
您可以对给定的 Context 对象安全地执行的常见操作取决于它最初的来源。
下面是 table 应用程序将接收上下文的常见位置,以及提到的每种情况的用法:
阅读本文希望对您有所帮助
https://possiblemobile.com/2013/06/context/
我喜欢从视觉上考虑 context
,因为我是 Fragments
的狂热用户,所以大多数时候我传递 context
,或继承 [=10] =] 实例通常来自 Activity
。
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
如 Android Developers 所述。它基本上可以为您提供帮助,确保您可以执行 "up-calls for application-level operations",所以让我们在您的案例中详细说明这一点。
I know it is needed BUT why not just be created and android handle the
context for me ?
您的 TextView
的新实例,创建时没有 Context
实例。所以它看起来像 TextView tv = new TextView();
会混淆 Android 关于 TextView
的生成位置。他们如何知道这是应用程序级别还是 activity 级别? TextView
在创建实例之前需要哪些特征?当您转到 TextView
的构造函数时,您会注意到它在生成所述 TextView
的新实例之前如何需要一些重要信息。例如: final Resources.Theme theme = context.getTheme();
只是一行,他们通过 Context
实例收集信息,现在他们知道信息来自哪里,然后他们可以应用相应的主题。
除非您告诉他们,Android 怎么知道您从哪里调用了 class 以及您希望将其应用于哪个主题?
最后回答你的问题,“为什么不直接创建 android 为我处理上下文?” 这个 IS android 为您处理事情,但它需要知道您来自哪里以及您处于生命周期的哪个阶段。
编辑: 从评论中添加。
why not that theme initialized in the application context and used in the code of the textview without my interference
因为它再次归结为您希望该小部件所基于的位置。假设我想要 activity
中的 TextView
但我们正在调用应用程序级上下文,自动应用的主题将是 @style/apptheme
。但是如果我希望 TextView
在当前 activity
遵循相同的样式指南,而不是手动更改我想创建的每个小部件的主题(我是开发人员)android 会处理它为您服务,无论您在应用程序中的哪个位置。它使样式更简单,创建 TextView
简单等的新实例等
you know i remember a scenario from .NET platform when i create new button on the form and without any thing passed to the constructor it inherits its theme for the parent form automatically .. you think .NET design is better on this or what?
不幸的是,我没有使用 .NET 的经验,但我认为在使用可以随时关闭和打开的应用程序期间,活动、服务和接收器的状态不断变化。这是一项很好的功能,可以确保 Android 知道您所在的位置以及您正在创建的内容。
我知道 android 中有很多这样的上下文
- 申请
- Activity
- 服务
- 内容提供者
- 广播接收器
所以当我创建一个 new TextView
时,我必须像这样在构造函数中传递 context
TextView textView = new TextView(this);
我知道它是必需的,但为什么不直接创建并 android 为我处理上下文?
我认为这与 Context
的生命周期有关。例如,Activity
可以终止并进行垃圾收集,这将使它的 Context
为空。这也可以反过来工作。在某些情况下,引用 Activity
的上下文会导致内存泄漏,并且 Activity
永远不会被垃圾回收。
也看看 this answer
上下文是记忆的方式 management.It 可能是 Android 应用程序中使用最多的元素……它也可能是最常被滥用的元素。
您可以对给定的 Context 对象安全地执行的常见操作取决于它最初的来源。
下面是 table 应用程序将接收上下文的常见位置,以及提到的每种情况的用法: 阅读本文希望对您有所帮助 https://possiblemobile.com/2013/06/context/
我喜欢从视觉上考虑 context
,因为我是 Fragments
的狂热用户,所以大多数时候我传递 context
,或继承 [=10] =] 实例通常来自 Activity
。
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
如 Android Developers 所述。它基本上可以为您提供帮助,确保您可以执行 "up-calls for application-level operations",所以让我们在您的案例中详细说明这一点。
I know it is needed BUT why not just be created and android handle the context for me ?
您的 TextView
的新实例,创建时没有 Context
实例。所以它看起来像 TextView tv = new TextView();
会混淆 Android 关于 TextView
的生成位置。他们如何知道这是应用程序级别还是 activity 级别? TextView
在创建实例之前需要哪些特征?当您转到 TextView
的构造函数时,您会注意到它在生成所述 TextView
的新实例之前如何需要一些重要信息。例如: final Resources.Theme theme = context.getTheme();
只是一行,他们通过 Context
实例收集信息,现在他们知道信息来自哪里,然后他们可以应用相应的主题。
除非您告诉他们,Android 怎么知道您从哪里调用了 class 以及您希望将其应用于哪个主题?
最后回答你的问题,“为什么不直接创建 android 为我处理上下文?” 这个 IS android 为您处理事情,但它需要知道您来自哪里以及您处于生命周期的哪个阶段。
编辑: 从评论中添加。
why not that theme initialized in the application context and used in the code of the textview without my interference
因为它再次归结为您希望该小部件所基于的位置。假设我想要 activity
中的 TextView
但我们正在调用应用程序级上下文,自动应用的主题将是 @style/apptheme
。但是如果我希望 TextView
在当前 activity
遵循相同的样式指南,而不是手动更改我想创建的每个小部件的主题(我是开发人员)android 会处理它为您服务,无论您在应用程序中的哪个位置。它使样式更简单,创建 TextView
简单等的新实例等
you know i remember a scenario from .NET platform when i create new button on the form and without any thing passed to the constructor it inherits its theme for the parent form automatically .. you think .NET design is better on this or what?
不幸的是,我没有使用 .NET 的经验,但我认为在使用可以随时关闭和打开的应用程序期间,活动、服务和接收器的状态不断变化。这是一项很好的功能,可以确保 Android 知道您所在的位置以及您正在创建的内容。