如何实现可以通过普通活动、列表活动和地图活动扩展的通用活动?
我想在我的应用程序的所有活动上显示相同的选项菜单。我创建了一个实现菜单的通用活动,并且我所有的进一步活动都扩展了它。
问题:当我需要扩展其他特定活动(例如 ListActivity 和 MapActivity)时,我无法弄清楚如何扩展通用活动并将 List 或 Map 行为添加到新类中。为了解决这个问题,我必须创建三个不同的通用活动,每个活动都扩展 Activity、ListActivity 和 MapActivity。
我尝试创建一个抽象活动,但它不起作用,我需要同时扩展两个类。我可以尝试接口,但由于我无法实现方法,我必须将菜单实现粘贴到整个二级类上,对吧?
I want to display the same options menu on all of my application's activities. I created a generic Activity that implements the menu, and all my further activies extend it.
The problem: when I need to extend other specific activities, like ListActivity and MapActivity, I can't figure out how to extend the generic activity and add the List or Map behaviour to the new class. To deal with the issue I had to create three different generic activities, each extending Activity, ListActivity and MapActivity.
I've tried creating an abstract activity but it doesn't work, I would need to extend two classes at the same time. I could try interfaces but since I can't implement methods, I would have to paste the menu implementation all over the second-level classes, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能这样做。 Java 不允许多重继承。
当我需要这种行为并且它取决于 Activity 生命周期时,我只需将其复制为两个抽象类:
AbstractActivity
AbstractMapActivity
您还可以阅读更多内容关于多重继承:
You can't do this. Java doesn't allow multiple inheritance.
When I need this kind of behavior and it depends on the Activity lifecycle I just replicate it two abstract classes:
AbstractActivity
AbstractMapActivity
You can also read more about multiple inheritance: