Skip to content

Commit 298bd9c

Browse files
Merge pull request react-native-maps#1130 from mattshen/master
issue#939, fix multiple-instance memory leak
2 parents 09c6d9d + 02a547e commit 298bd9c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

android/src/main/java/com/airbnb/android/react/maps/AirMapManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,12 @@ void pushEvent(ThemedReactContext context, View view, String name, WritableMap d
291291
.receiveEvent(view.getId(), name, data);
292292
}
293293

294+
295+
296+
@Override
297+
public void onDropViewInstance(AirMapView view) {
298+
view.doDestroy();
299+
super.onDropViewInstance(view);
300+
}
301+
294302
}

android/src/main/java/com/airbnb/android/react/maps/AirMapView.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.widget.RelativeLayout;
2222

2323
import com.facebook.react.bridge.LifecycleEventListener;
24+
import com.facebook.react.bridge.ReactContext;
2425
import com.facebook.react.bridge.ReadableArray;
2526
import com.facebook.react.bridge.ReadableMap;
2627
import com.facebook.react.bridge.WritableMap;
@@ -79,7 +80,9 @@ public class AirMapView extends MapView implements GoogleMap.InfoWindowAdapter,
7980
private final ScaleGestureDetector scaleDetector;
8081
private final GestureDetectorCompat gestureDetector;
8182
private final AirMapManager manager;
83+
private LifecycleEventListener lifecycleListener;
8284
private boolean paused = false;
85+
private boolean destroyed = false;
8386
private final ThemedReactContext context;
8487
private final EventDispatcher eventDispatcher;
8588

@@ -255,7 +258,7 @@ public void onCameraChange(CameraPosition position) {
255258
// updating location constantly, killing the battery, even though some other location-mgmt
256259
// module may
257260
// desire to shut-down location-services.
258-
LifecycleEventListener lifecycleListener = new LifecycleEventListener() {
261+
lifecycleListener = new LifecycleEventListener() {
259262
@Override
260263
public void onHostResume() {
261264
if (hasPermissions()) {
@@ -274,11 +277,15 @@ public void onHostPause() {
274277
//noinspection MissingPermission
275278
map.setMyLocationEnabled(false);
276279
}
277-
paused = true;
280+
synchronized (AirMapView.this) {
281+
AirMapView.this.onPause();
282+
paused = true;
283+
}
278284
}
279285

280286
@Override
281287
public void onHostDestroy() {
288+
AirMapView.this.doDestroy();
282289
}
283290
};
284291

@@ -290,6 +297,24 @@ private boolean hasPermissions() {
290297
checkSelfPermission(getContext(), PERMISSIONS[1]) == PackageManager.PERMISSION_GRANTED;
291298
}
292299

300+
/*
301+
onDestroy is final method so I can't override it.
302+
*/
303+
public synchronized void doDestroy() {
304+
if (lifecycleListener != null && context != null) {
305+
context.removeLifecycleEventListener(lifecycleListener);
306+
lifecycleListener = null;
307+
}
308+
if(!paused) {
309+
onPause();
310+
}
311+
if (!destroyed) {
312+
onDestroy();
313+
destroyed = true;
314+
}
315+
316+
}
317+
293318
public void setRegion(ReadableMap region) {
294319
if (region == null) return;
295320

0 commit comments

Comments
 (0)