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
Table of contents 1. Characteristics of JS 1.1 Mu...
Colleagues often ask, when deleting files/directo...
Since the entire application needs to be deployed...
Table of contents 1. Installation and introductio...
Preface The concept of dark mode originated from ...
Payment countdown to return to the home page case...
When nginx configures proxy_pass, the difference ...
1. Download MySQL 1.1 Download address https://do...
Install First you need to install Java and Scala,...
Pure front-end implementation:切片上傳斷點續傳.斷點續傳needs ...
By default, setting width for label and span is in...
1. Remove backslashes through the "stripslas...
<br />Related articles: 9 practical suggesti...
Table of contents Container Hierarchy The process...
JavaScript can do a lot of great things. This art...