How to implement a generic activity that can be extended by normal, List and Map Activities?

Deadly 提交于 2019-12-12 14:10:08

问题


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?


回答1:


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:

  • Multiple Inheritance in Java
  • Simulate multiple inheritance in Java


来源:https://stackoverflow.com/questions/3924464/how-to-implement-a-generic-activity-that-can-be-extended-by-normal-list-and-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!