保持SplashScreen直到在android中加载反应本机包

Ilj*_*lja 6 android splash-screen android-inflate react-native

我在本文中如何将启动画面添加到React Native App中的 android指南

并且在我的主要活动之前启动我的SplashScreen活动,即Android应用程序正在膨胀时.

到目前为止一切都那么好,但是有一些白色的闪烁是由我希望删除的本机js捆绑加载引起的.文章建议使用react-native-splash-screen库,但是我想将我的依赖关系保持在最低限度.

React原生文档有这个专业提示,基本上实现了我正在寻找的东西,但在iOS(它不会显示启动画面而捆绑未加载).我想弄清楚我将如何在原生java中为android做类似的事情,但到目前为止还没有运气.

Ser*_*nko 6

Here a way to do it when content is added by 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());
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

As you can see, the idea is to override stuff to hook into needed moment.

You could see other stuff to hook into in those classes, but in general react loads bundle asynchronously with jni functions, so I am not sure there's much to do there. You could override a chain

  1. MainApplication (assign mReactNativeHost) ->
  2. ReactNativeHost.createReactInstanceManager (copy original method but call ReactInstanceManagerBuilder.setJSBundleLoader) ->
  3. JSBundleLoader (wrap original JSBundleLoader.createAssetLoader into custom subclass, which would call inner loader in loadScript and then also call CatalystInstanceImpl.callFunction) ->
  4. CatalystInstanceImpl.PendingJSCall to just run what you need to run in there.

In other words, it's horrible and still React does not guarantee that PendingJSCall won't be run before bundle loads.