我在这篇文章中遵循了android指南How to Add a Splash Screen to a React Native App
并达到了在我的主要 Activity 之前启动我的SplashScreen Activity 的程度,即android应用正在膨胀时。
到目前为止,一切都很好,但是由于我要删除的 native js bundle 加载导致白色闪烁。文章建议使用react-native-splash-screen
库,但是我想将依赖性降到最低。
React native 文档具有这个Pro Tip,基本上可以实现我想要的功能,但是在iOS中(在未加载bundle的情况下,它始终显示启动屏幕)。我试图弄清楚我将如何在适用于Android的 native Java中执行类似的操作,但到目前为止还没有运气。
最佳答案
这是当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 负载是通过jni函数异步进行的,因此我不确定在这方面有很多事情要做。您可以覆盖一条链
MainApplication
(分配mReactNativeHost
)-> ReactNativeHost.createReactInstanceManager
(复制原始方法,但调用ReactInstanceManagerBuilder.setJSBundleLoader
)-> JSBundleLoader
(将原始JSBundleLoader.createAssetLoader
包装到自定义子类中,这将在loadScript
中调用内部加载程序,然后再调用CatalystInstanceImpl.callFunction
)-> CatalystInstanceImpl.PendingJSCall
只需运行您需要在其中运行的内容即可。 换句话说,这太可怕了,React仍然不能保证在 bundle 软件加载之前不会运行
PendingJSCall
。