在非活动课需要上下文 [英] Needing Context in non-Activity classes
问题描述
我已经在我的应用程序的一些类,需要调用,需要上下文作为参数的Android功能。 我没有把它作为类不是Activity类的子类。
什么是解决这个问题的正确方法是什么?
- 在把它作为每次调用的参数?
- 在它传递的类的实例,并保持它?
这取决于类的角色。但无论如何,通过的ApplicationContext
而不是活动之一。如果传递活动方面GC不能从内存中删除后,当你不需要的活动了。但是,应用程序上下文中使用,而应用程序并没有被OS.Refer 避免内存泄漏
I have some classes within my application that need to call Android functions that require the Context as a parameter. I don't have it as the class is not a subclass of the Activity class.
What is the correct way to tackle this problem?
- Pass it as a parameter on each call?
- Pass it at class instantiation and keep it?
It depends on the role of the class. But anyway pass ApplicationContext
but not Activity one. If you pass Activity context gc can't remove it from the memory when after you don't need activity anymore. But application context is used while application was not finished by OS.Refer Avoid Memory Leaks
这篇关于在非活动课需要上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!