在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
导读:本文以 工程创建步骤可以参考官网。本文所分析 我们知道上述工程是一个安卓应用,打开 安卓应用的启动流程是:在启动第一个 MainApplicationpublic class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // 其它对 packages 的操作 return packages; } @Override protected String getJSMainModuleName() { return "index"; } } @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); initializeFlipper(this, getReactNativeHost().getReactInstanceManager()); }
1.创建成员变量
2.在 onCreate 中:
在这里简要介绍下 ReactNativeHost
ReactInstanceManager 该类为核心类,主要负责管理 JS 的加载、维护生命周期、管理 JS 与 C++ 的交互等等。可以把 MainActivity接着看 public class MainActivity extends ReactActivity { @Override protected String getMainComponentName() { return "myProject"; } }
public abstract class ReactActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, PermissionAwareActivity { private final ReactActivityDelegate mDelegate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDelegate.onCreate(savedInstanceState); }
protected void onCreate(Bundle savedInstanceState) { String mainComponentName = getMainComponentName(); mReactDelegate = new ReactDelegate( getPlainActivity(), getReactNativeHost(), mainComponentName, getLaunchOptions()) { @Override protected ReactRootView createRootView() { return ReactActivityDelegate.this.createRootView(); } }; if (mMainComponentName != null) { loadApp(mainComponentName); } } 这里首先创建了 ReactDelegate 实例。接着来看 protected void loadApp(String appKey) { mReactDelegate.loadApp(appKey); getPlainActivity().setContentView(mReactDelegate.getReactRootView()); } 由此走到 public void loadApp(String appKey) { if (mReactRootView != null) { throw new IllegalStateException("Cannot loadApp while app is already running."); } mReactRootView = createRootView(); mReactRootView.startReactApplication( getReactNativeHost().getReactInstanceManager(), appKey, mLaunchOptions); } 在这里做了三件事:创建 rootView ( createRootView首先看下什么是 rootView。 public class ReactRootView extends FrameLayout implements RootView, ReactRoot { /* ... */} ReactRootView 继承自 getReactInstanceManager
protected ReactInstanceManager createReactInstanceManager() { ReactInstanceManagerBuilder builder = /* ... */ for (ReactPackage reactPackage : getPackages()) { builder.addPackage(reactPackage); } String jsBundleFile = getJSBundleFile(); if (jsBundleFile != null) { builder.setJSBundleFile(jsBundleFile); } else { builder.setBundleAssetName(Assertions.assertNotNull(getBundleAssetName())); } ReactInstanceManager reactInstanceManager = builder.build(); return reactInstanceManager; } 此处做的事情如下:
startReactApplicationpublic void startReactApplication(/* */) { // ... try { // ... mReactInstanceManager.createReactContextInBackground(); } finally { // ... } } 最终执行到
详细分析我们放到另一篇文章:React Native startReactApplication 流程梳理。 总结总结本文,通过
到此这篇关于React Native 启动流程简析的文章就介绍到这了,更多相关React Native 启动内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论