Skip to content

Commit

Permalink
Merge pull request react-native-maps#694 from felipecsl/v0.10-stable
Browse files Browse the repository at this point in the history
Fixes crash during Activity onPause()
  • Loading branch information
Spike Brehm authored Oct 14, 2016
2 parents f45e755 + 1a0d3aa commit 8994e30
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ protected AirMapView createViewInstance(ThemedReactContext context) {
return new AirMapView(context, this.appContext.getCurrentActivity(), this, this.googleMapOptions);
}

@Override
public void onDropViewInstance(AirMapView view) {
view.doDestroy();
super.onDropViewInstance(view);
}

private void emitMapError(String message, String type) {
WritableMap error = Arguments.createMap();
error.putString("message", message);
Expand Down
81 changes: 31 additions & 50 deletions android/src/main/java/com/airbnb/android/react/maps/AirMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.TileOverlay;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -76,24 +75,24 @@ public class AirMapView extends MapView implements GoogleMap.InfoWindowAdapter,
private final ScaleGestureDetector scaleDetector;
private final GestureDetectorCompat gestureDetector;
private final AirMapManager manager;
private LifecycleEventListener lifecycleListener;
private boolean paused = false;
private final ThemedReactContext context;
private final EventDispatcher eventDispatcher;

public AirMapView(ThemedReactContext context, Context appContext, AirMapManager manager, GoogleMapOptions googleMapOptions) {
public AirMapView(ThemedReactContext reactContext, Context appContext, AirMapManager manager,
GoogleMapOptions googleMapOptions) {
super(appContext, googleMapOptions);

this.manager = manager;
this.context = context;
this.context = reactContext;

super.onCreate(null);
super.onResume();
super.getMapAsync(this);

final AirMapView view = this;
scaleDetector =
new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
new ScaleGestureDetector(reactContext, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
view.startMonitoringRegion();
Expand All @@ -102,7 +101,7 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
});

gestureDetector =
new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() {
new GestureDetectorCompat(reactContext, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
view.startMonitoringRegion();
Expand All @@ -123,13 +122,13 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
this.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (!AirMapView.this.paused) {
if (!paused) {
AirMapView.this.cacheView();
}
}
});

eventDispatcher = context.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
}

@Override
Expand Down Expand Up @@ -230,36 +229,32 @@ public void onCameraChange(CameraPosition position) {
// updating location constantly, killing the battery, even though some other location-mgmt
// module may
// desire to shut-down location-services.
lifecycleListener = new LifecycleEventListener() {
@Override
public void onHostResume() {
if (hasPermissions()) {
//noinspection MissingPermission
map.setMyLocationEnabled(showUserLocation);
}
synchronized (AirMapView.this) {
AirMapView.this.onResume();
paused = false;
}
}
LifecycleEventListener lifecycleListener = new LifecycleEventListener() {
@Override
public void onHostResume() {
if (hasPermissions()) {
//noinspection MissingPermission
map.setMyLocationEnabled(showUserLocation);
}
synchronized (AirMapView.this) {
AirMapView.this.onResume();
paused = false;
}
}

@Override
public void onHostPause() {
if (hasPermissions()) {
//noinspection MissingPermission
map.setMyLocationEnabled(false);
}
synchronized (AirMapView.this) {
AirMapView.this.onPause();
paused = true;
}
}
@Override
public void onHostPause() {
if (hasPermissions()) {
//noinspection MissingPermission
map.setMyLocationEnabled(false);
}
paused = true;
}

@Override
public void onHostDestroy() {
AirMapView.this.doDestroy();
}
};
@Override
public void onHostDestroy() {
}
};

context.addLifecycleEventListener(lifecycleListener);
}
Expand All @@ -269,20 +264,6 @@ private boolean hasPermissions() {
checkSelfPermission(getContext(), PERMISSIONS[1]) == PackageManager.PERMISSION_GRANTED;
}

/*
onDestroy is final method so I can't override it.
*/
public synchronized void doDestroy() {
if (lifecycleListener != null) {
context.removeLifecycleEventListener(lifecycleListener);
lifecycleListener = null;
}
if (!paused) {
onPause();
}
onDestroy();
}

public void setRegion(ReadableMap region) {
if (region == null) return;

Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 8994e30

Please sign in to comment.