ReactNative多bundle加载

package com.meicai.react.bridge.activity;

import android.os.Bundle;
import android.view.KeyEvent;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import com.meicai.react.bridge.BuildConfig;

import java.util.List;

/**
 * Created by 付晓龙
 * on 2020-01-10
 */
public class MCNewBundleActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {
    ReactInstanceManager mReactInstanceManager;
    ReactNativeHost reactNativeHost;
    ReactRootView mReactRootView;

    @Override 
    public void invokeDefaultOnBackPressed() {
        super.invokeDefaultOnBackPressed();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReactRootView = new ReactRootView(this);
        reactNativeHost = new ReactNativeHost(getApplication()) {
            @Override
            public boolean getUseDeveloperSupport() {
                return false;
            }

            @Override
            protected List<ReactPackage> getPackages() {
                return null;
            }
        };
        mReactInstanceManager= reactNativeHost.getReactInstanceManager();
        //---------------
         mReactInstanceManager = ReactInstanceManager.builder().setApplication(getApplication())
                .setCurrentActivity(this)
                .setBundleAssetName("index.fxl.android.bundle")
                .setJSMainModulePath("index")
                .addPackage(new MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        mReactRootView.startReactApplication(mReactInstanceManager, "SecondApp");
        //----------
        setContentView(mReactRootView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostPause(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostDestroy(this);
        }
        if (mReactRootView != null) {
            mReactRootView.unmountReactApplication();
        }
    }

    @Override
    public void onBackPressed() {
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onBackPressed();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager.showDevOptionsDialog();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

}


你可能感兴趣的:(ReactNative多bundle加载)