From ebe98c8990308bba857b529c4321956bd8d3c9be Mon Sep 17 00:00:00 2001 From: Meibes <26258451+Meibes@users.noreply.github.com> Date: Sun, 26 Jan 2020 16:39:44 +0100 Subject: [PATCH] add onDetach-method to MapScaleBarLayer to free resources automatically removed mapScaleBar.destroy() methods from onDestroy-Activity methods changed global mapScaleBar-variables to local --- .../src/org/oscim/android/test/GettingStarted.java | 5 +---- .../src/org/oscim/android/test/MapsforgeActivity.java | 11 +---------- .../src/org/oscim/android/test/SimpleMapActivity.java | 11 +---------- vtm/src/org/oscim/scalebar/MapScaleBarLayer.java | 6 ++++++ 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/vtm-android-example/src/org/oscim/android/test/GettingStarted.java b/vtm-android-example/src/org/oscim/android/test/GettingStarted.java index d39d6a3f..5913e015 100644 --- a/vtm-android-example/src/org/oscim/android/test/GettingStarted.java +++ b/vtm-android-example/src/org/oscim/android/test/GettingStarted.java @@ -42,7 +42,6 @@ public class GettingStarted extends Activity { private static final String MAP_FILE = "berlin.map"; private MapView mapView; - private MapScaleBar mapScaleBar; @Override public void onCreate(Bundle savedInstanceState) { @@ -69,7 +68,7 @@ public class GettingStarted extends Activity { mapView.map().setTheme(VtmThemes.DEFAULT); // Scale bar - mapScaleBar = new DefaultMapScaleBar(mapView.map()); + MapScaleBar mapScaleBar = new DefaultMapScaleBar(mapView.map()); MapScaleBarLayer mapScaleBarLayer = new MapScaleBarLayer(mapView.map(), mapScaleBar); mapScaleBarLayer.getRenderer().setPosition(GLViewport.Position.BOTTOM_LEFT); mapScaleBarLayer.getRenderer().setOffset(5 * CanvasAdapter.getScale(), 0); @@ -94,8 +93,6 @@ public class GettingStarted extends Activity { @Override protected void onDestroy() { - if (mapScaleBar != null) - mapScaleBar.destroy(); mapView.onDestroy(); super.onDestroy(); } diff --git a/vtm-android-example/src/org/oscim/android/test/MapsforgeActivity.java b/vtm-android-example/src/org/oscim/android/test/MapsforgeActivity.java index db225f36..b4a9583e 100644 --- a/vtm-android-example/src/org/oscim/android/test/MapsforgeActivity.java +++ b/vtm-android-example/src/org/oscim/android/test/MapsforgeActivity.java @@ -65,7 +65,6 @@ public class MapsforgeActivity extends MapActivity { private static final Tag SEA_TAG = new Tag("natural", "sea"); private TileGridLayer mGridLayer; - private DefaultMapScaleBar mMapScaleBar; private Menu mMenu; private boolean mS3db; private VectorTileLayer mTileLayer; @@ -93,14 +92,6 @@ public class MapsforgeActivity extends MapActivity { SELECT_MAP_FILE); } - @Override - protected void onDestroy() { - if (mMapScaleBar != null) - mMapScaleBar.destroy(); - - super.onDestroy(); - } - public static class MapFilePicker extends FilePicker { public MapFilePicker() { setFileDisplayFilter(new FilterByFileExtension(".map")); @@ -197,7 +188,7 @@ public class MapsforgeActivity extends MapActivity { mMap.layers().add(new BuildingLayer(mMap, mTileLayer)); mMap.layers().add(new LabelLayer(mMap, mTileLayer)); - mMapScaleBar = new DefaultMapScaleBar(mMap); + DefaultMapScaleBar mMapScaleBar = new DefaultMapScaleBar(mMap); mMapScaleBar.setScaleBarMode(DefaultMapScaleBar.ScaleBarMode.BOTH); mMapScaleBar.setDistanceUnitAdapter(MetricUnitAdapter.INSTANCE); mMapScaleBar.setSecondaryDistanceUnitAdapter(ImperialUnitAdapter.INSTANCE); diff --git a/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java b/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java index 9de50ea8..f749a251 100644 --- a/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java +++ b/vtm-android-example/src/org/oscim/android/test/SimpleMapActivity.java @@ -38,7 +38,6 @@ import org.oscim.theme.ThemeLoader; import org.oscim.theme.VtmThemes; public class SimpleMapActivity extends BaseMapActivity { - private DefaultMapScaleBar mapScaleBar; BuildingLayer mBuildingLayer; private boolean mShadow; @@ -71,7 +70,7 @@ public class SimpleMapActivity extends BaseMapActivity { groupLayer.layers.add(new LabelLayer(mMap, mBaseLayer)); mMap.layers().add(groupLayer); - mapScaleBar = new DefaultMapScaleBar(mMap); + DefaultMapScaleBar mapScaleBar = new DefaultMapScaleBar(mMap); mapScaleBar.setScaleBarMode(DefaultMapScaleBar.ScaleBarMode.BOTH); mapScaleBar.setDistanceUnitAdapter(MetricUnitAdapter.INSTANCE); mapScaleBar.setSecondaryDistanceUnitAdapter(ImperialUnitAdapter.INSTANCE); @@ -86,14 +85,6 @@ public class SimpleMapActivity extends BaseMapActivity { mMap.setTheme(VtmThemes.DEFAULT); } - @Override - protected void onDestroy() { - if (mapScaleBar != null) - mapScaleBar.destroy(); - - super.onDestroy(); - } - void runTheMonkey() { themes[0] = ThemeLoader.load(VtmThemes.DEFAULT); themes[1] = ThemeLoader.load(VtmThemes.OSMARENDER); diff --git a/vtm/src/org/oscim/scalebar/MapScaleBarLayer.java b/vtm/src/org/oscim/scalebar/MapScaleBarLayer.java index a4235794..25d32c2c 100644 --- a/vtm/src/org/oscim/scalebar/MapScaleBarLayer.java +++ b/vtm/src/org/oscim/scalebar/MapScaleBarLayer.java @@ -62,4 +62,10 @@ public class MapScaleBarLayer extends Layer implements Map.UpdateListener { mapScaleBar.redrawNeeded = false; } + + @Override + public void onDetach() { + super.onDetach(); + mapScaleBar.destroy(); + } }