This is the situation:
I have an activity with a PagerAdapter containing three fragments. This activity allows you to change the screen orientation.
One of those fragments has mapView created with the osmdroid library, and this fragment is retained (setRetainInstance(true)). At each orientation changed, the event onCreateView is fired again:
@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;
}
At start everything works fine, but when I rotate the phone the sentence "mLocationOverlay = new MyLocationOverlay(getActivity(), mapView);" is executed again, and this means that, when you close the application, the GPS icon never disappears.
If you do not rotate the phone the icon disappears when the application closes.
This is what I have on the onPause event:
@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 evento onCreateView hago este sencillo control el problema desaparece:
If I put this control in the onCreateView event, the problem disappears:
if (this.getActivity().isFinishing())
mLocationOverlay = new MyLocationOverlay(getActivity(), mapView);
But then, among other things, I am not able to center anymore:
mapController.animateTo(mLocationOverlay.getMyLocation());
Any idea? There's something I'm doing wrong?
If you need more code, you have no more to say. Thank you!