10

I have written one MapActivity class that is able to display a set of places as well as single places. On startup, the application creates an instance of this MapActivity and displays multiple places. If the user clicks on a certain place, then a new Activity is launched that shows the details of the selected place. This activity has a menu item that allows the user to view the place on a map - this causes that a new instance of the MapActivity is created, except that now only this single place is displayed.

The problem now is that if the user navigates back to the first MapActivity (the one that shows multiple places) the tiles won't be loaded anymore + sometimes OutOfMemoryErrors are encountered.

According to the Android JavaDocs, it is only possible to have one MapActivity per process. However, I don't want to define my MapActivity as a singleInstance/singleTask, since the user should always be able to navigate back to the first MapActivity that shows multiple places.

I have seen that the Google Places app (which has come with Google Map 4.4) for Android uses multiple MapActivity instances. How is this possible? And how can I achieve it in my application?

rds
  • 26,253
  • 19
  • 107
  • 134
stacky
  • 103
  • 1
  • 1
  • 5

4 Answers4

20

According to the Android JavaDocs, it is only possible to have one MapActivity per class

It's not one map view per class, it's per process.

It's known that you might experience some issues when using multiple mapviews in one process. Usually this is the case (your app running in one process) if you don't configure anything specific. You could though use the android:process attribute in your manifest to assign to your acitivites:

<activity android:name=".activity.directory.MapView1" android:process=":MapView1">

<activity android:name=".activity.directory.MapView2" android:process=":MapView2">

This way you have the activities running in separate processes, which works well if you don't use any shared static variables across activities.

Also see the discussion on the bug in the android bug tracker:

http://code.google.com/p/android/issues/detail?id=3756

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • Thanks for your answer. I already knew about the possibility to use the android:process attribute. However, in my case I have just one MapActivity defined in the AndroidManifest.xml file, which is able to display multiple places as well as single places. I could now split this single MapActivity into two MapActivities (as you suggest), but I wonder if there are any other solutions - I don't think that in the Google Places app for Android each MapActivity instance is running in a separate process. So far, I prefer to re-design my application s.t. always only one MapActivity is active. – stacky Aug 01 '10 at 09:34
  • Looking at the logfile while running Google Places, the activities are all within the same process, I can confirm that. Nevertheless, I wouldn't assume that the Google Apps are entirely built based on the public SDK components only but have some non-public custom components as well; i.e. which is why Google Maps uses a different map tiles provider and is more accurate than the MapView of the SDK, etc. The process approach is the common one when working with the SDK and up-to-date the only workaround I know of, also referring to the Android dev group and Android SDK bug tracker. – Mathias Conradt Aug 01 '10 at 09:56
  • My application contains tab layout(TabHost, TabSpec). I am using two map views in my application. So if I am enabling satellite view in one of map view then other map is also shown in satellite view, if I drag one map towards USA other map view is also is dragged towards USA no matter where it was earlier pointing. I used the above solution and but it is not solving my problem. What can the problem? – AndroidDev Jun 08 '12 at 14:03
  • I created a simple application. An activity containing button to start remote activity. Pressing the Button creates another remote activity I can see it in DDMS. But when it comes to my tab(TabHost, TabSpec) application It is not working!!! – AndroidDev Jun 08 '12 at 14:09
4

You can have more than one MapActivity per process as long as only one is running at a time. Each MapActivity can have only one MapView assigned to it. You can show different maps by reusing the same MapView in the same MapActivity. You can reuse a MapView by declaring it as a static class variable, removing it from the view that it was currently added to when you are done displaying it, giving it new GPS coordinates and adding it to the next view. When you are finished with that MapActivity finish it and then you can open a new MapActivity. This works, I do in my app.

Danny Remington - OMS
  • 5,244
  • 4
  • 32
  • 21
2

Another simple solution: just override onDestroy method of all of your MapActivities to prevent closing resources, e.g.:

    @Override
    protected void onDestroy() {
        super.onStop();
    }

We should invoke onStop here to prevent RuntimeException thrown. Yes, that's a hack but it works.

Flavio
  • 6,205
  • 4
  • 30
  • 28
1

Update:

The Google Maps Android API v2 has support for Fragments. This means that you can easily display multiple maps like you would any other fragment.

See the documentation and read more about the new MapFragment class.

Chris Broadfoot
  • 5,082
  • 1
  • 29
  • 37