这是这种情况:
我有一个包含三个片段的 PagerAdapter 活动。此活动允许您更改屏幕方向。
其中一个片段使用 osmdroid 库创建了 mapView,并且保留了该片段 ( setRetainInstance(true) )。在每次更改方向时,都会再次触发 onCreateView 事件:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView = (LinearLayout) inflater.inflate(R.layout.main,
container, false);
mapView = (MapView) fragmentView.findViewById(R.id.practice_mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setUseSafeCanvas(true);
setHardwareAccelerationOff();
mapController = (MapController) mapView.getController();
[...]
mLocationOverlay = new MyLocationOverlay(getActivity(), mapView);
mLocationOverlay.setDrawAccuracyEnabled(true);
[...]
mapView.invalidate();
return fragmentView;
}
开始时一切正常,但是当我旋转手机时,会再次执行语句“ mLLocationOverlay = new MyLocationOverlay(getActivity(), mapView); ”,这意味着当您关闭应用程序时,GPS 图标永远不会消失。
如果您不旋转手机,该图标会在应用程序关闭时消失。
这就是我在 onPause 事件中的内容:
@Override
public void onPause() {
mLocationOverlay.disableCompass();
if (this.getActivity().isFinishing()) {
disableMyLocation();
releaseLocationOverlay();
}
cleanMapViewCache();
}
private void disableMyLocation() {
if (mLocationOverlay.isMyLocationEnabled() == true)
mLocationOverlay.disableMyLocation();
if (mLocationOverlay.isFollowLocationEnabled() == true)
mLocationOverlay.disableFollowLocation();
}
private void releaseLocationOverlay() {
if (mLocationOverlay != null) {
mapView.getOverlays().remove(mLocationOverlay);
mLocationOverlay = null;
}
}
private void cleanMapViewCache() {
mapView.getTileProvider().clearTileCache();
System.gc();
}
Si en el eventto onCreateView hago este sencillo control el problema desaparece:
如果我把这个控件放在 onCreateView 事件中,问题就消失了:
if (this.getActivity().isFinishing())
mLocationOverlay = new MyLocationOverlay(getActivity(), mapView);
但是,除其他外,我无法再居中:
mapController.animateTo(mLocationOverlay.getMyLocation());
任何想法?有什么我做错了吗?
如果您需要更多代码,则无需多说。谢谢!