I’m working on an android application that currently has two Activities, A and B, that have some code in common, their options menu and some other code. To cut down on copying and pasting, I created a class called BaseActivity that extends Activity with the, and I then had A and B extend BaseActivity. At least that’s what I wanted to do, but I ran into a problem. While A now extends BaseActivity just fine, I can’t figure out how to make B extend BaseActivity because it’s not just an activity, it’s a MapActivity. So it’s inheritance tree looks like this:
MapActivity inheritance tree:
java.lang.Object
android.content.Context
android.content.ContextWrapper
android.view.ContextThemeWrapper
android.app.Activity
com.google.android.maps.MapActivity
But in order for it to inherit my code, I’d need to make it look like this:
java.lang.Object
android.content.Context
android.content.ContextWrapper
android.view.ContextThemeWrapper
android.app.Activity
mypackage.BaseActivity
com.google.android.maps.MapActivity
Is there any way to insert my custom class in an inheritance tree?
No, you can’t. It’s hard to say exactly what the best approach would be here, but I would at least try to use composition instead of inheritance. Can you implement a separate non-activity object and use one of those from both A and B?