Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Technique for running multiple MapActivities in the same process

It is well know to people using Google Maps in an Android app that they can only use one MapActivity per process. There is a bug discussing this in detail along with the workaround of putting each MapActivity in a separate process. I don't want to do this in my app for a few reason and have developed another workaround that seems to work.

For each Activity that contains a map, I have it extend ActivityGroup and create/destroy the MapActivity in onResume/onPause as a child activity. This ensures there will only be one instance of a MapActivity at a time (assuming you have one Activity showing at a time). Below is a simplified version of my implementation:

public class MyMapActivityGroup extends ActivityGroup {

    @Override
    protected void onResume() {
        super.onResume();

        addMapView();
    }

    @Override
    protected void onPause() {
        super.onPause();

        removeMapView();
    }

    private void addMapView() {
        Intent intent = new Intent(this, MyMapActivity.class);
        Window window = getLocalActivityManager().startActivity("map", intent);
        setContentView(window.getDecorView());
    }

    private void removeMapView() {
        setContentView(new FrameLayout(this));
        getLocalActivityManager().removeAllActivities();
    }

}

The MapActivity I am using is nothing special and doesn't require any modification. It just sets a MapView as its content view.

This seems to work fine for me. But is there any downside to doing this? My main concern is a memory leak created when going between activities containing a map.

like image 817
Jason Hanley Avatar asked Feb 25 '11 14:02

Jason Hanley


1 Answers

I would guess that the only reason to not do this would be performance. The map activity can already be a bit of a dog, especially when starting it, so if you find yourself allocating and deallocating the view frequently, this might perform pretty poorly. However, its really dependent on how often the view will be created and removed, which depends entirely on behavioral aspects of your application.

like image 73
Nick Campion Avatar answered Oct 16 '22 20:10

Nick Campion