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

我怕爱的太早我们不能终老 提交于 2019-12-03 17:15:01

问题


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.


回答1:


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();
  }
}


来源:https://stackoverflow.com/questions/21815818/map-of-mapfragment-gets-loaded-with-lag-when-returning-from-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!