0

I've created a map fragment as seen here MapView in a Fragment (Honeycomb) and it works great.

The issue is when i have another map fragment in the following activity.

Activity A has a MapFragment taking up the entire page. Activity B has a MapFragment that takes up only half the page (the bottom half has a ListFragment). Both work fine but when i click the back button to go from Activity B back to A the map view only takes up half the page and the bottom half of the page has refresh issues.

The mapviews state isn't saved independent of the other either. For example: When going from Activty A to Activity B the map displays the same. This is fine. But if the user pans around or zooms the map in activity B. When the user goes back to Activity A the map doesn't go back to where it was when that activity was paused (This obviously isn't a major issue as i can manually save the state and reposition. would be handy if it was handled for me though).

This is how Activity A looks after returning back from Activity B Incorrect Refreshed Mapview

Community
  • 1
  • 1
Stimsoni
  • 3,166
  • 2
  • 29
  • 22

2 Answers2

1

You asked to let you know if there's a better solution - there is now. Google released Android Maps API v2 which gives you MapFragment. Now it should work like a charm.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
0

I found a solution, but please let me know if you can find a better one.

The problem is when activity A is resumed it's fragments are not meaning they are never redrawn and maintain the size that it had in Activity B.

To resolve the issue i had to add the below method to the MapFragment and call that from the activities onResume(). This ensures the map is resized to take up the space it originally did. Yes it's a massive hack so if you find a way to ensure fragments are created, destroyed and resumed correctly and reliably please post an answer here.

/**
 * This method should be called in the activity containing the fragments onResume method
 * to ensure the mapview in this fragment has the expected size.
 *
 * @param width
 * @param height
 */
public void setMapViewSize(int width, int height){
    LayoutParams lp = new LayoutParams(width, height);
    myMapView.setLayoutParams(lp);
}

Just for clarification, when i say MapFragment i'm talking about the class that extends MapActivity and not the class that loads the activity that contains the map (extends ActivityHostFragment).

Stimsoni
  • 3,166
  • 2
  • 29
  • 22