的getContext(),getApplicationContext(),getBaseContext()和QUOT之间的区别,这与QUOT;这与、区别、getApplicationContext、

2023-09-11 11:16:12 作者:你真不怕我离去i

的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.

推荐答案

View.getContext():认为目前运行在返回的背景下,一般当前活动的活动。

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

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.

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().