Introduction: This article takes the sample project (Android part) created by The steps for project creation can refer to the official website. The We know that the above project is an Android application. Open the source code file in The startup process of an Android application is as follows: a globally unique 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(); // Other operations on 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. Create an instance of the member variable
2. In onCreate:
Here is a brief introduction to ReactNativeHost ReactInstanceManager This class is the core class, which is mainly responsible for managing JS loading, maintaining the life cycle, managing the interaction between JS and C++, and so on. MainActivity Next, look at public class MainActivity extends ReactActivity { @Override protected String getMainComponentName() { return "myProject"; } } Only the getMainComponentName method is overridden in 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); } } Here, the ReactDelegate instance is first created. Next, let's look at protected void loadApp(String appKey) { mReactDelegate.loadApp(appKey); getPlainActivity().setContentView(mReactDelegate.getReactRootView()); } From here we go to the 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); } Three things are done here: create rootView ( createRootViewFirst, let’s take a look at what rootView is. public class ReactRootView extends FrameLayout implements RootView, ReactRoot { /* ... */} ReactRootView inherits from 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; } Here’s what’s happening:
startReactApplicationpublic void startReactApplication(/* */) { // ... try { // ... mReactInstanceManager.createReactContextInBackground(); finally // ... } } Finally, the execution is carried out into the
We will put the detailed analysis in another article: React Native startReactApplication process analysis. Summarize To summarize this article, we take the sample project (Android part) created by The main function of The main functions of
This is the end of this article about the brief analysis of the React Native startup process. For more relevant React Native startup content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Analysis of the principle and creation method of Mysql temporary table
>>: Solution to the problem of IP loss caused by copying centos8 virtual machine under VMWARE
1. Introduction to Varnish Varnish is a high-perf...
The difference between run and start in docker Do...
1. Download the MySQL installation package First ...
During this period of time, I was studying docker...
This article uses an example to describe the MySQ...
Recently, WeChat was forced by Apple to develop a...
I recently started learning Linux. After reading ...
When creating a MySQL container with Docker, some...
【author】 Liu Bo: Senior Database Manager at Ctrip...
MySQL itself does not support recursive syntax, b...
1 System Installation Steps OS Version:1804 Image...
First download the zip archive version from the o...
Overview Today we will mainly share how to config...
Table of contents environment summary Module Func...
When using Docker containers, it is more convenie...