Starting from the cryptic error: java.lang.AssertionError: Attached DialogModule to host with pending alert but no FragmentManager (not attached to an Activity).
I have this in MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myManager = new MyManager(this);
myManager.start();
}
public void onManagerReady() {
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setJSBundleFile("assets://index.js")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactCode");
setContentView(mReactRootView);
}
...
Calling onManagerReady from onCreate works as expected. But when I use an AsyncTask inside the manager, it fails:
public interface ManagerHandler { void onJobReady(); }
...
public class Manager implements ManagerHandler {
public MyManager(Context context) {
this.context = context;
}
public void start() {
new AsyncJob(context, this).execute("");
}
@Override
public void onJobReady() {
((MainActivity)context).onManagerReady();
}
private class AsyncJob extends AsyncTask<String, Void, String> {
private final Context context;
private final ManagerHandler handler;
public AsyncJob(Context context, ManagerHandler handler) {
this.context = context;
this.handler = handler;
}
@Override
protected void onPostExecute(String result) {
handler.onJobReady();
}
...
}
I omitted some AsyncTask boilerplate - let me assure you that the AsyncTask is working and that onManagerReady() is getting called at the proper time.
I also tried messing with threads and "run on ui thread", no luck. It's interesting to note that even using Thread.sleep() messes React, even without AsyncTask or calls from/to other files.
it's probably too late to help you, but maybe someone else will find this answer useful.
Basically your MainActivity is a subclass of ReactActivity. You need to replace it with your own class similar to ReactActivity (just copy-paste). Change name of method onCreate(Bundle savedInstanceState) to something else, and call this method when rest of your app is ready for react.
Also you need to change onResume(), to change state only when react is loaded:
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mLifecycleState = LifecycleState.RESUMED;
mReactInstanceManager.onResume(this, this);
}
}
Call onResume at the end of method where you initialize react.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With