How to use multiple MapActivities/MapViews per Android application/process

Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.