Destroy MapView

假如想象 提交于 2019-12-08 08:49:21

问题


I have a MapActivity where you can switch between MapView (Google Maps) and OfflineMapView (my class, shows a map previously downloaded to SD card). When switching between maps I want to completely destroy and create the map views so that only one map view is in the memory. I want this for 2 reasons:

  1. My OfflineMapView takes most of available memory for caching tiles.
  2. Google MapView has some attached threads which I don't want running when OfflineMapView is visible.

I tried to remove the MapView from layout and null my reference to it but when I want to show it again I get an exception saying that MapActivity can have only one MapView.

EDIT: The presence of Google MapView (visibility is set to GONE) doesn't have any effect on OfflineMapView FPS. I didn't get any OutOfMemoryErrors either.


回答1:


Use ActivityGroup as your Activity class and have it start and stop sub-activities for each type of map. For example, to get the view for the Google Map:

Intent intent = new Intent(this, GoogleMapActivity.class);
Window window = getLocalActivityManager().startActivity("google-map", intent);
View googleMapView = window.getDecorView();
container.addView(googleMapView);

to destroy it:

container.removeView(googleMapView);
getLocalActivityManager().removeAllActivities();

and do the same with your offline map. This should completely stop the MapActivity and it's threads.

Note that I have found LocalActivityManager.destroyActivity() to be buggy, so I used LocalActivityManager().removeAllActivities() in the example since it does work for this case.



来源:https://stackoverflow.com/questions/5133298/destroy-mapview

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