diff --git a/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java b/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java index 300f369fb9..892c220c21 100644 --- a/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java +++ b/OsmAnd/src/net/osmand/plus/activities/IntermediatePointsDialog.java @@ -156,15 +156,22 @@ public class IntermediatePointsDialog { } } if (cnt > 0) { - for (int i = checkedIntermediates.length - 1; i >= 0; i--) { - if (!checkedIntermediates[i]) { - cnt--; - app.getTargetPointsHelper().removeWayPoint((MapActivity) (mapActivity instanceof MapActivity?mapActivity : null), cnt == 0, - i == checkedIntermediates.length - 1? -1 : i); + boolean changeDestinationFlag =!checkedIntermediates [checkedIntermediates.length - 1]; + if(cnt == checkedIntermediates.length){ //there is no alternative destination if all points are to be removed? + app.getTargetPointsHelper().removeAllWayPoints((MapActivity) (mapActivity instanceof MapActivity?mapActivity : null), true); + }else{ + for (int i = checkedIntermediates.length - 2; i >= 0; i--) { //skip the destination until a retained waypoint is found + if (checkedIntermediates[i] && changeDestinationFlag) { //Find a valid replacement for the destination + app.getTargetPointsHelper().makeWayPointDestination((MapActivity) (mapActivity instanceof MapActivity?mapActivity : null), cnt == 0, i); + changeDestinationFlag = false; + }else if(!checkedIntermediates[i]){ + cnt--; + app.getTargetPointsHelper().removeWayPoint((MapActivity) (mapActivity instanceof MapActivity?mapActivity : null), cnt == 0, i); + } + } + if(mapActivity instanceof MapActivity) { + ((MapActivity) mapActivity).getMapLayers().getContextMenuLayer().setLocation(null, ""); } - } - if(mapActivity instanceof MapActivity) { - ((MapActivity) mapActivity).getMapLayers().getContextMenuLayer().setLocation(null, ""); } } }