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

StackOverflow https://stackoverflow.com/questions/3924464

  •  29-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top