14

Is there a way to display a MapView without extending MapActivity? I have other Activity class which I'm extending and I would prefer not to change that... I've seen that you can inflate using MapActivity, but didn't find any spec/examples on how to do it.

Michal Dymel
  • 4,312
  • 3
  • 23
  • 32
  • my own, which is setting different things, has lot of functions, objects I use in the activities – Michal Dymel Jun 10 '10 at 11:45
  • I could extend my class with MapActivity, but that would mean all my activities would extend it... – Michal Dymel Jun 10 '10 at 12:02
  • Had the same kinda problem. Ended up that another activity that doesn't even display a MapView now extends MapActivity. But seems okay, no overhead / strange behaviour detected yet. – icyerasor Mar 05 '11 at 18:55

3 Answers3

6

Is there a way to display a MapView without extending MapActivity?

Not that I am aware of.

I could extend my class with MapActivity, but that would mean all my activities would extend it

Then you need to refactor your code, such that your activities do not all inherit from your own base class. Inheritance is not a very flexible OO technique, particularly in Java.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    I find it very unfortunate that some "official" Android components require your activity to inherit a specific base class, with MapActivity and FragmentActivity being two specifically annoying examples. – Ralf Jul 30 '12 at 16:40
  • "Then you need to refactor your code, such that your activities do not all inherit from your own base class. Inheritance is not a very flexible OO technique, particularly in Java." It's dissapointing that precisely the Android SDK doesn't apply this – User Nov 07 '12 at 15:30
  • I mean, this is just so wrong and annoying. Just because you put a map view somewhere, the whole activity has to extend MapActivity. It makes sense to put the common code of all my activities in 1 common superclass, not to avoid inheritance, just because Android is using it incorrectly. What's next, maybe a TextViewActivity? – User Nov 22 '12 at 20:00
  • And as if that were not enough, Android allows only 1 map activity in the app/process (not the topic of this thread, but anyways). What's wrong with maps in Android?? – User Nov 22 '12 at 20:01
0

Right now you can use fragments to show the map in any place of your activity and your main activity doesn't have to extend mapactivity. you have an implementation in MapView in a Fragment (Honeycomb)

Community
  • 1
  • 1
Jokin
  • 4,188
  • 2
  • 31
  • 30
0

A practical way is to make two versions of your original base activity and the only difference between them is one extends Activity and the other extends MapActivity (the same problem with ListActivity).

There're duplicate codes. That's bad smell I know. But they're almost identical, you could simply copy the whole file and make a tiny modification when you have changed the base activity.

Roland
  • 1,109
  • 13
  • 15