简体   繁体   English

Android库项目-定义类

[英]Android Library Project - Defining Classes

Hey! 嘿! (Apologies for the long post here) (这里很长的道歉)

I am writing a few applications which will be based off of essentially the same code. 我正在编写一些基于基本相同代码的应用程序。

Each application will have its differences, some will have new activities, all will have different resources assigned. 每个应用程序都有其差异,一些应用程序有新的活动,所有应用程序都有不同的资源分配。

I am attempting to use an Android Library project so that I can place all of the common code in one place, so manjor changes only have to be made once and all projects will be updated. 我试图使用一个Android Library项目,以便可以将所有通用代码放在一个地方,因此主要更改只需进行一次即可,所有项目都将被更新。

Now, the resources are an easy solution, as I can just define each resouce in the main application and these will override the ones in the library project as long as naming conventions stay the same. 现在,资源是一个简单的解决方案,因为我可以在主应用程序中定义每个资源,只要命名约定保持不变,这些资源将覆盖库项目中的资源。

Now, my question is how do I do the same but for classes? 现在,我的问题是,除了上课,我该怎么做? For example, ill have a base view as a wrapper for common code across all views. 例如,ill有一个基本视图作为所有视图中通用代码的包装。 in this, i launch various activities. 为此,我开展了各种活动。 What if all three applications need to launch a different activity as they will contain different UI and features? 如果这三个应用程序都需要启动不同的活动,因为它们将包含不同的UI和功能,该怎么办? How would I go about this? 我将如何处理? The library project will never know that the main project wants to use a different activity, as its already been compiled. 库项目永远不会知道主项目想要使用其他活动,因为它已经被编译。

I have a couple of solutions I have thought up, but I want to know if anyone else has some ideas? 我已经考虑过几种解决方案,但是我想知道是否还有其他人有想法? It would be of great help for any feedback here: 对于这里的任何反馈都会有很大的帮助:

  1. The Activity in the Library Project that is to be launched stores a reference to the class to be loaded, which will be defined in the main application at its launch. 库项目中将要启动的活动存储了对要加载的类的引用,该引用将在启动时在主应用程序中定义。 This was when the library project calls this activity, it can load what its been told to. 这是当图书馆项目调用此活动时,它可以加载所告知的内容。

  2. I leave all functionality that will not change inside the library projects activity. 我将所有不会更改的功能留在库项目活动中。 I then extend this activity, and override the onCreate to do what I need here (such as loading different layouts, setting up views, etc). 然后,我扩展此活动,并覆盖onCreate来执行我在这里需要的操作(例如加载不同的布局,设置视图等)。 Now this has a drawback, as I have to define most of the entry points in my main application rather than in the library project. 现在这有一个缺点,因为我必须在主应用程序而不是库项目中定义大多数入口点。

I hope you follow what my issue is here. 希望您关注我的问题。 Please ask if I have explained this poorly. 请问我是否对此解释不佳。

Thanks, 谢谢,

Adam 亚当

Or more simple: Should you use inheritance or composition to achieve that kind of flexibility? 或更简单:您应该使用继承或组合来实现这种灵活性吗?

I had this kind of structure quest as well. 我也有这种结构上的追求。 Usually you would solve it by reusing a applicable pattern & break the necessary code parts in smaller pieces. 通常,您可以通过重用适用的模式来解决它,并将必要的代码部分分成较小的部分。 Ie if you know the difference are views, then extract that into some distinguished classes that will implement against an common interface and refer to the interface in your base activities. 即,如果您知道差异是视图,则将其提取到将针对通用接口实现的某些杰出的类中,并在基础活动中引用该接口。

Some links for you: 一些链接为您:

At least from how I understand your question, this is more of a OOP Design question. 至少从我对您的问题的理解来看,这更多是一个OOP设计问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM