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