0

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();
}
ozmank
  • 763
  • 1
  • 8
  • 23

2 Answers2

6

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().

Austin
  • 201
  • 1
  • 4
2

Overlays are automatically removed when your activity is finished. You have some other bug in your code. For example, you may be testing this by pressing HOME, which does not trigger onDestroy().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491