Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes crash during Activity onPause() #694

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -75,24 +74,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 @@ -101,7 +100,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 @@ -122,13 +121,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 @@ -222,36 +221,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 @@ -261,20 +256,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