“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
A, preparation,
1.1 Creating a Project
Before moving on to the development of HarmonyOS, you can use a simple HelloWorld example code to understand the flow of the runtime. Here is a simple sample HelloWorld code structure created by DevEco Studio, followed by an analysis of the startup process.
1.2 Running Projects
Run HelloWorld above and it will look like this:
Analyze the startup process
2.1 Startup Process
A simple HelloWorld application from HarmonyOS can be divided into the following stages:
2.2 Parsing the config.json File
The config.json file is the main configuration file for the HarmonyOS application. It is located in Entry -> SRC -> main -> config.json. HarmonyOS (Hongmon) — Config. json
2.2 the initialization
Under the module of initialization is mainly through the config. Json package and class name to locate com. Example. Demo. MyApplication.
MyApplication will look like this, and it will do some application initialization
package com.example.demo; import ohos.aafwk.ability.AbilityPackage; public class MyApplication extends AbilityPackage { @Override public void onInitialize() { super.onInitialize(); }}Copy the code
2.4 Access Ability Full class name
The full class name of entry Ability is also configured in the config.json file
MainAbility corresponds to the configuration information about abilities in config.json
MainAbility main content is as follows, in the main interface will load interface, through super. SetMainRoute (MainAbilitySlice. Class. GetName ()); HarmonyOS (Great Ability) — Ability and Page (HarmonyOS)
package com.example.demo; import com.example.demo.slice.MainAbilitySlice; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; public class MainAbility extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setMainRoute(MainAbilitySlice.class.getName()); }}Copy the code
2.5 operation Ability
This step is to start the above class
2.6 Run the subinterface of Ability
Executing the onStart method after MainAbility is run loads the child page, which in this case is MainAbilitySlice, the main contents of which are shown below.
package com.example.demo.slice; import com.example.demo.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; public class MainAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}Copy the code
In MainAbilitySlice we find that onStart loads UI configuration related to class resourcetable. Layout_ability_main, ResourceTable is a constant class dynamically generated when HarmonyOS starts. It assigns a flag ID to each file such as XML. To view this class, run the application and look it up in the class file
2.7 Loading an XML file to display the content
The content in the sub-page can be configured through XML files. This method of configuring content style based on XML is also convenient and easy to unified management and system parsing.
So much for a HelloWorld application from HarmonyOS.