React Native: Is it possible to persist native Java variable values on app reload?

Brien Crean :

Is it possible to maintain the value of Java variables in a custom react native module when reloading the JS? I would like to maintain components in the below code for debugging purposes. It persists on onHostResume if the app goes into the background but on reload the value is lost.

public class CustomModule extends ReactContextBaseJavaModuleWithEvents implements LifecycleEventListener {

    public List<JsonObject> components = new ArrayList<>();

    public CustomModule(ReactApplicationContext reactContext) {
        super(reactContext);
        reactContext.addLifecycleEventListener(this);
    }

    @ReactMethod
    void addComponents(component) {
        // add some components...
        components.add(component);
    }

    @Override
    public String getName() {
        return "CustomModule";
    }

    @Override
    public void onHostResume() {
        getReactApplicationContext()
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit("DEBUG_TAG", components.toString());
    }

}
Brien Crean :

Every reload the JS code is creating a new instance of CustomModule reinitializing components. I should have set the components List as a static class variable so that it is only initialized once.

public class CustomModule extends ReactContextBaseJavaModuleWithEvents implements LifecycleEventListener {

    public static List<JsonObject> components = new ArrayList<>();

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162485&siteId=1