Search code examples
androidandroid-mapviewgoogle-maps-android-api-2supportmapfragment

Android: Google Maps API v2 rendering issue with markers and camera animation


EDIT

I've found a better STR:

  1. Make sure to set "Do not keep activities" in Developer options in Settings.
  2. Open the app with a SupportMapFragment as a child fragment of another fragment.
  3. Switch to another app
  4. Open your app again
  5. Notice you can't interact with the map and no animations work.
  6. Open another screen within the app
  7. Notice there's a single frame or so of the map with the markers drawn on screen.

I have an issue with Google Maps API v2.

I am animating the camera to zoom to a set of custom marker bitmaps rendered on a MapFragment.

On selecting one of these marker tooltips I open a geo: intent (for the Google Maps app etc.)

When the user presses back it reopens my activity with the fragment back stack rebuilt.

My issue is that it doesn't render camera animations or the markers on coming back, though there is a brief display of those markers when the user presses back to go to the previous fragment.

The GoogleMap instance has the markers, but it doesn't render them - I'm guessing because the MapFragment/MapView thinks it doesn't need to be rendered.

How do I force a render of the MapView? Failing that, how do I get the MapView to recognise that the model has changed?


Solution

  • The issue was to do with the way I was handling my child fragments.

    Every time the parent fragment would call onCreate the children fragments would get recreated.

    I did the following to handle my child fragments, but there may be a better way:

    private static final String TAG_FRAGMENT_MAP = "TagFragmentMap";
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
        if (savedInstanceState == null) {
            // create the fragments for the first time
            ft.add(R.id.view_flip, new SupportMapFragment(), TAG_FRAGMENT_MAP);
            ft.commit();
        }
    }
    
    // ...
    
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        mMapFragment = (SupportMapFragment)findFragmentByTag(TAG_FRAGMENT_MAP);
    }