开发者

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

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 MapAct开发者_JS百科ivity.

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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜