Search code examples
androidmapsandroid-mapviewoverlays

Removing overlays from mapView


I create overlays in mapView , the problem is when I get back to the mapsview activity again, i see the overlays that i previously made. i have tried to remove overlays in ondestroy method which doesnt help.Tried all, trust me.heres the code...Also in onCreate() i have used mapView.invalidate(); which doesnt help either. I just want to get rid of overlays when the activity is finished. Any solution?

   @Override
protected void onDestroy(){ 
    super.onDestroy();
    stopWorker=true;
    mapView.invalidate();
    mapView.postInvalidate();
    for (int i=0; i<mapView.getOverlays().size(); i++ ) {
        mapView.getOverlays().remove(i);
    }

    Toast.makeText(this,"map destroy ...", Toast.LENGTH_LONG).show();
    mapView.getOverlays().clear();
}

Solution

  • Seems like your activity is not getting Finished. Only if the Activity is finished, it will enter the onDestroy state. Try writing the same code in onPause() or onResume().