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

Multiple instances getting created #28

Open
veris-NikhilSrivastava opened this issue Jun 9, 2020 · 3 comments
Open

Multiple instances getting created #28

veris-NikhilSrivastava opened this issue Jun 9, 2020 · 3 comments

Comments

@veris-NikhilSrivastava
Copy link

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 the onNotificationOpened() 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 the android:launchMode="singleTop" in AndroidManifest.xml

Please if any one could throw some light on this.

Thanks

@tal32123
Copy link

+1

@mathBordin
Copy link

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?

@esinanturan
Copy link

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);
            }
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants