从库班进入主体工程类 [英] Access the main project class from a library class
问题描述
我有一个使用库项目(我全部建成)的Android项目。我使用的ADT / SDK V14和我需要访问主项目的主类,并调用一个函数驳回在库项目中的对话框时。现在,我可以做,如果我将引用添加到我的主要项目库项目,但效果不理想。如何从一个类在库项目中获得了对类在我的主要项目?
这是它是如何工作的:
- 在我有我的主类在我的项目,是一个tabhost
- 的tabhost得到碎片从库项目 标签
- 一个片段的选项卡启动一个DialogFragment
- 在当前的DialogFragment被驳回,我需要调用主类fillItems()函数(这是我卡)
任何人有什么想法?
谢谢, 埃德
我如何从一个类的库项目的引用类在我的主要项目?
在理想情况下,你不知道。
相反,您:
- 定义中包含要调用的实现来自于主体工程的方法库的接口
- 在实施上的一些可能的类接口的主项目
- 在供应是通过一些二传手或者通过构造函数参数实现图书馆
- 在有需要的库调用的接口实现的方法
- 确保你没有做这一切引进任何形式的垃圾收集问题
要字面意思是从一个类的库项目的引用类在我的主要项目的唯一方法是通过反射,这是缓慢的,使得难以维持code
I have an Android project that uses a library project (all of which I built). I am using ADT/SDK v14 and I need to access the main class in the main project and call a function when a dialog in the library project is dismissed. Now, I can do that if I add a reference to my main project to the library project, but that isn't ideal. How do I get a reference to a class in my main project from a class in the library project?
This is how it's working:
- I have my main class in my project that is a tabhost
- The tabhost gets the fragments for the tabs from the library project
- One of the fragments for the tabs launches a DialogFragment
- When that DialogFragment is dismissed, I need to call a fillItems() function in the main class(this is where I'm stuck)
Anyone have any ideas?
Thanks, Ed
How do I get a reference to a class in my main project from a class in the library project?
Ideally, you don't.
Instead, you:
- Define an interface in the library that contains the methods you want to invoke whose implementation comes from the main project
- Implement that interface on some likely class in your main project
- Supply that implementation to the library via some setter or via a constructor argument
- Have the library call the methods on the interface implementation as needed
- Make sure you aren't introducing any sort of garbage collection problems by doing all of this
The only way to literally "get a reference to a class in my main project from a class in the library project" is via reflection, which is slow and makes for difficult-to-maintain code.
这篇关于从库班进入主体工程类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!