Map of MapFragment gets loaded with lag when returning from another activity
Asked Answered
F

1

10

As far as I can see, MapFragment has an issue with transition animations. All views on the layout are getting shown immediately, including the MapFragment's own views (like zoom buttons). But the map itself gets loaded with a lag only after the animation is completed.

In order to illustrate the problem, I did the following:

  1. I changed one of the Activities in the Google maps android API examples slightly. It opens a blank activity via an Action Item. When I click back button, the map gets loaded, but only after the transition is completed.
  2. I exaggerated the transition effect a little bit, so that you can see the problem better. I set the transition animation speed in Developer Options to 5x. Even on 1x speed, this lag is disturbing though.

See this video: http://www.youtube.com/watch?v=12SEotktlXI

Do you have any suggestion to prevent this lag? Why do all views get loaded immediately but the map itself doesn't?

Testing environment: Nexus 5, Android 4.4.2, unrooted

Edit: This problem also occures when MapView is used instead of MapFragment.

Fritillary answered 16/2, 2014 at 19:56 Comment(4)
Have tried to test it on other devices? I noticed it only on Nexus 5.. Galaxy S3 showed different behavior...Terrill
Yes, definitely. I tried it on HTC Desire SV, and there was no lag. I think that's because they don't use transition animations. When I turn transition animations off on my Nexus 5 (settings > developer options > transition animations), the lag disappears too.Fritillary
So it's something related to KitKat... maybe a bug.Terrill
I think it's a maps API bug because all other views get loaded immediately but only the map doesn't...Fritillary
S
-3

Reason: Its because as soon as you settings activity will be shown, the map activity will be on its onpause() state; thus, I assume android management reclaimed the memory from the map activity.

Solution: Make a static class and declare you map statically there, to avoid android from reclaiming the memory used by your map.

Ex.

//your static class
public class MapData{
  public static GoogleMap map;
}

//your map activity
public class MapActivity extends Activity{

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(MapData.map != null)
       MapData.map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  }
}
Slavic answered 26/2, 2014 at 4:7 Comment(2)
At least make sure that onDestroy the static var is cleaned up.Herring
Sorry but that didn't work. I replaced every occurence of mMap in RetainMapDemoActivity example with MapData.map but the map still has lag when I come back from the dummy activity... :(Fritillary

© 2022 - 2024 — McMap. All rights reserved.