2121import android .widget .RelativeLayout ;
2222
2323import com .facebook .react .bridge .LifecycleEventListener ;
24+ import com .facebook .react .bridge .ReactContext ;
2425import com .facebook .react .bridge .ReadableArray ;
2526import com .facebook .react .bridge .ReadableMap ;
2627import 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