getApplicationContext()和getActivity()之间的区别 [英] Difference between getApplicationContext() and getActivity()
问题描述
在Android中,getApplicationContext()和getActivity()与 this 有什么区别?
What is the difference between getApplicationContext() and getActivity() and this in Android?
推荐答案
之间有很多区别:
View.getContext():返回视图当前正在其中运行的上下文.通常是当前活动的Activity.
View.getContext(): Returns the context the view is currently running in. Usually the currently active Activity.
Activity.getApplicationContext():返回整个应用程序的上下文(所有Activity在其中运行的进程).如果您需要一个与整个应用程序的生命周期相关的上下文,而不仅仅是当前的Activity,请使用此上下文代替当前的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.可以通过getBaseContext()访问从ContextWrapper内部引用的Context.
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().
也请检查:
https://stackoverflow.com/a/10641257/4018207 https://developer.android.com/reference/android/view/View.html#getContext%28%29
这篇关于getApplicationContext()和getActivity()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!