Ho risolto questo provvisoriamente salvando un riferimento statico al ReactInstanceManager
sulla classe MainActivity
, e aggiungendo un metodo public static getContext()
. Non sembra giusto, ma sembra funzionare.
public final class MainActivity extends ReactActivity {
private static ReactInstanceManager sReactInstanceManager = null;
/* [...] */
@Override
protected ReactInstanceManager createReactInstanceManager() {
ReactInstanceManager manager = super.createReactInstanceManager();
sReactInstanceManager = manager;
return manager;
}
public static ReactContext getContext() {
if (sReactInstanceManager == null){
// This doesn't seem to happen ...
throw new IllegalStateException("Instance manager not available");
}
final ReactContext context = sReactInstanceManager.getCurrentReactContext();
if (context == null){
// This really shouldn't happen ...
throw new IllegalStateException("React context not available");
}
return context;
}
/* [...] */
}
createReactInstanceManager()
è chiamato da ReactActivity
s' onCreate()
metodo, quindi suppongo che sia lecito ritenere che sReactInstanceManager
punterà alla istanza corretta, ma non prendere la mia parola per esso.