以下是通过React添加内容时的一种方法:
=== MainActivity.java ===
import com.facebook.react.*;
import android.content.Context;
import android.app.Activity;
import android.util.Log;
import android.view.View;
public class MainActivity extends ReactActivity {
class CustomReactActivityDelegate extends ReactActivityDelegate {
class CustomReactRootView extends ReactRootView {
public CustomReactRootView(Context context) {
super(context);
}
@Override
public void onViewAdded(View child) {
super.onViewAdded(child);
Log.d("React views started to appear", "Static js code has already run");
}
}
private Activity currentActivity;
public CustomReactActivityDelegate(Activity activity, String mainComponentName) {
super(activity, mainComponentName);
currentActivity = activity;
}
protected ReactRootView createRootView() {
return new CustomReactRootView(currentActivity);
}
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new CustomReactActivityDelegate(this, getMainComponentName());
}
...
}
正如你所看到的,这个想法是为了覆盖一些东西,以连接到所需的时刻。
您可以在这些类中看到其他要挂接的内容,但通常情况下,react loads捆绑包与jni函数是异步的,所以我不确定还有多少事情要做。你可以覆盖一条链子
-
MainApplication
(分配
mReactNativeHost
)->
-
ReactNativeHost.createReactInstanceManager
(复制原始方法,但调用
ReactInstanceManagerBuilder.setJSBundleLoader
)->
-
JSBundleLoader
(包裹原始文件
JSBundleLoader.createAssetLoader
进入自定义子类,该子类将调用
loadScript
然后再打电话
CatalystInstanceImpl.callFunction
)->
-
CatalystInstanceImpl.PendingJSCall
在那里跑你需要的东西。
换言之,这很可怕,但仍能做出反应并不能保证
PendingJSCall
在加载捆绑包之前不会运行。