首页 文章

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

提问于
浏览
6

我在本文中关注了android指南How to Add a Splash Screen to a React Native App

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

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

React原生文档有Pro Tip本质上实现了我想要的东西,但是在iOS中(它在未加载bundle时一直显示启动画面) . 我想弄清楚我将如何在原生java中为android做类似的事情,但到目前为止还没有运气 .

2 回答

  • 0

    这是在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());
        }
        ...
    }
    

    正如您所看到的,我们的想法是覆盖所需的时刻 .

    你可以在这些类中看到其他东西要挂钩,但一般来说,使用jni函数异步地加载bundle,所以我不确定那里有很多东西要做 . 你可以覆盖一个链

    • MainApplication (分配 mReactNativeHost ) - >

    • ReactNativeHost.createReactInstanceManager (复制原始方法但调用 ReactInstanceManagerBuilder.setJSBundleLoader ) - >

    • JSBundleLoader (将原始 JSBundleLoader.createAssetLoader 包装到自定义子类中,这将在 loadScript 中调用内部加载器,然后也调用 CatalystInstanceImpl.callFunction ) - >

    • CatalystInstanceImpl.PendingJSCall 只需运行你需要在那里运行的东西 .

    换句话说,它很糟糕,但React并不保证在捆绑加载之前不会运行 PendingJSCall .

  • 1

    使用rn-splash-scren . 当您的反应组件加载时,只需调用即可

    从“rn-splash-screen”导入SplashScreen; //隐藏活动的初始屏幕SplashScreen.hide();

    你需要从原生方面进行设置,但这很容易 .

相关问题