在非活动课需要上下文 [英] Needing Context in non-Activity classes

查看:133
本文介绍了在非活动课需要上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的应用程序的一些类,需要调用,需要上下文作为参数的Andr​​oid功能。 我没有把它作为类不是Activity类的子类。

什么是解决这个问题的正确方法是什么?

  1. 在把它作为每次调用的参数?
  2. 在它传递的类的实例,并保持它?
解决方案

这取决于类的角色。但无论如何,通过的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?

  1. Pass it as a parameter on each call?
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆