Skip to content

Commit 0f4b491

Browse files
committed
#fix-calling js function after bridge has been destroyed
1 parent 8e22a13 commit 0f4b491

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

android/lib/src/main/java/com/marianhello/bgloc/react/BackgroundGeolocationModule.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.facebook.react.bridge.Callback;
77
import com.facebook.react.bridge.LifecycleEventListener;
88
import com.facebook.react.bridge.ReactApplicationContext;
9+
import com.facebook.react.bridge.ReactContext;
910
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1011
import com.facebook.react.bridge.ReactMethod;
1112
import com.facebook.react.bridge.ReadableMap;
@@ -47,7 +48,7 @@ public class BackgroundGeolocationModule extends ReactContextBaseJavaModule impl
4748

4849
private BackgroundGeolocationFacade facade;
4950
private org.slf4j.Logger logger;
50-
private ReactApplicationContext currentContext;
51+
private ReactContext currentContext;
5152

5253
public static class ErrorMap {
5354
public static ReadableMap from(String message, int code) {
@@ -85,10 +86,10 @@ private static WritableMap from(Throwable e) {
8586

8687
public BackgroundGeolocationModule(ReactApplicationContext reactContext) {
8788
super(reactContext);
88-
reactContext.addLifecycleEventListener(this);
8989
currentContext = reactContext;
90+
currentContext.addLifecycleEventListener(this);
9091

91-
facade = new BackgroundGeolocationFacade(getContext(), this);
92+
facade = new BackgroundGeolocationFacade(currentContext, this);
9293
logger = LoggerManager.getLogger(BackgroundGeolocationModule.class);
9394
}
9495

@@ -129,6 +130,13 @@ public void onHostDestroy() {
129130
// facade = null;
130131
}
131132

133+
@Override
134+
public void onCatalystInstanceDestroy() {
135+
super.onCatalystInstanceDestroy();
136+
logger.info("Destroying plugin facade");
137+
facade.destroy();
138+
}
139+
132140
private void runOnBackgroundThread(Runnable runnable) {
133141
// currently react-native has no other thread we can run on
134142
new Thread(runnable).start();
@@ -325,9 +333,14 @@ public void forceSync(Callback success, Callback error) {
325333
}
326334

327335
private void sendEvent(String eventName, Object params) {
328-
currentContext
329-
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
330-
.emit(eventName, params);
336+
try {
337+
currentContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
338+
.emit(eventName, params);
339+
}
340+
catch (Exception e){
341+
logger.debug("error sending event: {}", e.toString());
342+
}
343+
331344
}
332345

333346
private void sendError(PluginException error) {
@@ -350,7 +363,7 @@ public int getAuthorizationStatus() {
350363
return facade.getAuthorizationStatus();
351364
}
352365

353-
public Context getContext() {
366+
public ReactContext getContext() {
354367
// return getReactApplicationContext().getBaseContext();
355368
return currentContext;
356369
}

0 commit comments

Comments
 (0)