Open veris-NikhilSrivastava opened 4 years ago
+1
Samething is happening here. After close the app and reopen it, it generates two instances, if I close it again and reopen, it generates more one instance and it goes on until I get a java.lang.OutOfMemoryError. Any ideas?
Because there is a bug in the module its not listening to destroy events of the app we can implement a module to listen and kill app or stop the service by that.
LifeCyleModule.java
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
public class lifeCycleModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
private static ReactApplicationContext reactContext;
lifeCycleModule(ReactApplicationContext context) {
super(context);
reactContext = context;
reactContext.addLifecycleEventListener(this);
}
@Override
public String getName() {
return "lifeCycleModule";
}
@Override
public void onHostResume() {
// Activity `onResume`
}
@Override
public void onHostPause() {
// Activity `onPause`
}
@Override
public void onHostDestroy() {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("DESTROY",null);
}
@ReactMethod
public void kill(){
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
LifeCylePackage.java
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class lifeCyclePackage implements ReactPackage {
public lifeCyclePackage() {
}
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new lifeCycleModule(reactContext));
return modules;
}
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
import {
NativeModules,
NativeEventEmitter,
} from 'react-native';
const {lifeCycleModule} = NativeModules;
const eventEmitter = new NativeEventEmitter(lifeCycleModule);
eventEmitter.addListener('DESTROY', (event) => {
try{
VIForegroundService.stopService();
lifeCycleModule.kill();
}catch (e) {
console.log(e);
}
});
Hi, So I was using a notifications library which has a function which detects that whether app is opened via notification or not (
onNotificationOpened()
). So, every time a foreground service is created, and the app is closed and if we tap on notification then theonNotificationOpened()
gets called as many times as the app is closed and opened when the foreground service is started. Seems like multiple instances are getting created. I've also set theandroid:launchMode="singleTop"
in AndroidManifest.xmlPlease if any one could throw some light on this.
Thanks