本项目是展示应用快启特性接入的实例。示例页面提供快启启动开关:开启后,应用可通过快启能力完成启动;关闭后,应用将按照常规冷启动流程启动。
| Files | Last commit | Last update |
|---|---|---|
| 5 months ago | ||
| 4 months ago | ||
| 5 months ago | ||
| 3 months ago | ||
| 17 days ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 3 months ago | ||
| 4 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago | ||
| 5 months ago |
Accelerated Application Startup Based on the Application Hyper Startup Feature
Overview
This sample demonstrates how to integrate the application hyper startup feature. The sample page provides a hyper startup switch. When the switch is turned on, the application can be started through the hyper startup capability. When the switch is turned off, the application is started through the common cold startup process.
To explicitly demonstrate the necessity and key points of hyper startup adaptation, this sample uses the color mode as the verification scenario. After hyper startup adaptation is complete, the application can correctly display the corresponding color after the color mode is switched and the application is started again through hyper startup. If the adaptation is not complete, the color status cannot be correctly synchronized, resulting in abnormal page display.
Preview
Switching the Color Mode in Hyper Startup Mode (Application Code Correctly Adapted) |
Switching the Color Mode in Hyper Startup Mode (Application Code Not Correctly Adapted) |
|
|---|---|---|
How to Use
-
Use DevEco Studio to open the project and configure the signature.
-
Ensure that srcEntry in entry/src/main/module.json5 points to the required AbilityStage.
- Application code that has been correctly adapted: ./ets/MyAbilityStageAdapted.ets
- Application code that has not been correctly adapted: ./ets/MyAbilityStage.ets
-
Build and run the debug version of the application. On the home page, you can turn on/off the dark color mode and hyper startup switch.
-
After enabling hyper startup, perform the hyper startup initialization process as prompted, and then compare the effects of cold startup and hyper startup.
Learning Procedure
- View entry/src/main/ets/pages/Index.ets to learn about the page switch entry and the configuration writing process.
- View entry/src/main/ets/MyAbilityStage.ets to observe the risky operations in the application version that has not been adapted to hyper startup.
- View entry/src/main/ets/MyAbilityStageAdapted.ets to compare the adaptation method of moving risky operations to after the onAboutToCreateAbility method.
- Modify srcEntry in entry/src/main/module.json5, switch between the adapted and non-adapted versions, and verify the differences.
- View the README file of the application adaptation point scanning tool to learn how to use the IDE plugin to scan for hyper startup risks.
Project Structure
HyperStartup/
├──AppScope
│ ├──resources
│ └──app.json5 // Application -level configuration, including bundleName, version, icon, and app name
├──entry
│ ├──src
│ │ ├──main
│ │ │ ├──ets
│ │ │ │ ├──constants
│ │ │ │ │ ├──AppConstants.ets // Common application constants
│ │ │ │ │ ├──AppStorageKeys.ets // AppStorage key name constants
│ │ │ │ │ └──PageStyleConstants.ets // Page style constants
│ │ │ │ ├──entryability
│ │ │ │ │ └──EntryAbility.ets // Application entry ability
│ │ │ │ ├──entrybackupability
│ │ │ │ │ └──EntryBackupAbility.ets
│ │ │ │ ├──pages
│ │ │ │ │ └──Index.ets // Main page demo, including the dark mode and hyper startup switch
│ │ │ │ ├──utils
│ │ │ │ │ └──Logger.ets
│ │ │ │ ├──MyAbilityStage.ets // AbilityStage implementation that is not adapted to hyper startup
│ │ │ │ └──MyAbilityStageAdapted.ets // AbilityStage implementation that has been adapted to hyper startup
│ │ │ ├──resources
│ │ │ └──module.json5 // Entry module configuration, where srcEntry is used to switch the adaptation version
│ │ ├──mock
│ │ ├──ohosTest
│ │ └──test
│ ├──build-profile.json5
│ ├──hvigorfile.ts
│ ├──obfuscation-rules.txt
│ └──oh-package.json5
├──hvigor
│ └──hvigor-config.json5
├──screenshots
│ ├──cold-start.gif
│ ├──cold-start-switch.gif
│ ├──demo_phone.png
│ ├──hyperstartup-adapted-switch.gif
│ ├──hyperstartup-start.gif
│ └──hyperstartup-unadapted-switch.gif
├──tools
│ ├──DevEco plugin
│ ├──images
│ ├──Static scanning tool
│ └──hyperstartupcheck-user-manual.md // README file for the tool for checking the hyper startup adaptation points
├──build-profile.json5 // Project-level build and signature configurations
├──code-linter.json5
├──hvigorfile.ts
├──LICENCE
├──oh-package-lock.json5
├──oh-package.json5
└──README.md
How to Implement
The overall process is as follows:
- On the home page, use @StorageLink to bind the dark mode and hyper startup switch status in AppStorage.
- When the user switches to the dark mode, Index.ets updates the page status and then writes DARK_MODE_KEY to Preferences.
- When the user switches to the hyper startup mode, Index.ets calls setHyperSnapEnabled(isEnabled) to update the system hyper startup switch and writes HYPER_STARTUP_ENABLED_KEY to Preferences.
- When the application is started next time, AbilityStage reads the configuration from Preferences and writes it back to AppStorage. The page then restores to the last display state.
- In the hyper startup scenario, the configuration must be read in the hyper startup process. Otherwise, the latest user settings may not be read.
Writing Page Configurations
The page entry is located in entry/src/main/ets/pages/Index.ets and contains two switches:
- Dark mode switch: Call toggleTheme (isDark) to update the page status corresponding to AppStorageKeys.IS_DARK_MODE and save the result to DARK_MODE_KEY in Preferences.
- Hyper startup switch: Call toggleHyperStartup (isEnabled), obtain the hyper startup management module through loadNativeModule('@ohos.app.ability.hyperSnapManager'), call setHyperSnapEnabled(isEnabled) to enable or disable hyper startup, and save the result to HYPER_STARTUP_ENABLED_KEY of Preferences.
After hyper startup is enabled, a message is displayed, prompting you to re-execute the hyper startup initialization process. Hyper startup can be performed only after the initialization is complete.
Hyper Startup Adaptation Principles
During the hyper startup initialization phase, some startup logic of the application is executed and saved. Therefore, any operation that depends on the actual runtime environment or external data is not suitable to be performed in the hyper startup initialization process. For example:
- Read or write disk data such as local files and Preferences.
- Access data that changes over time, such as network, database, and cloud configurations.
- Initiate an IPC call or register a listener that depends on the actual process status.
These operations should be performed in the hyper startup process. In this demo, the key risk is reading Preferences during the AbilityStage startup phase. If the dark mode configuration is read only during the initialization phase of hyper startup, the status used during the initialization phase will be retained for each subsequent hyper startup, instead of the status saved by the user last time.
The following APIs are involved in the adaptation:
- onAboutToCreateAbility: triggered for both cold startup and hyper startup. It is applicable to contain the service state that needs to be moved backward from the hyper startup initialization process.
- onLaunchFromHyperSnap: triggered only for hyper startup. It is applicable to process the logic that needs to be executed only in the hyper-startup scenario.
- setHyperSnapEnabled: sets whether to enable hyper startup. This is a common API. In this demo, onAboutToCreateAbility can be used for both cold startup and hyper startup.
Obtaining the Hyper Startup Adaptation Points
The adaptation points for hyper startup refer to risky operations that may be performed by an application during hyper startup initialization, such as file read/write, IPC calls, and network access. If these adaptation points are not handled, the application may have issues such as dirty data. You can use the DevEco Studio code scanning tool to obtain the adaptation points. For details, see README file of the adaptation point scanning tool.
Comparison Before and After Adaptation
This project provides two AbilityStage files for you to directly compare the differences before and after adaptation.
MyAbilityStage.ets (Unadapted)
- In onCreate, loadColorMode() is called to directly read the dark mode configuration from Preferences.
- onCreate is called before the hyper startup. Reading Preferences is a disk access operation, which may cause risks during hyper startup.
- During hyper startup, the reading logic in onCreate is not re-executed based on the common cold startup process. As a result, the dark mode set by the user may not take effect after the application is restarted.
MyAbilityStageAdapted.ets (Adapted)
- In onCreate, only the lightweight logic that can be executed during the hyper startup initialization process is retained, and external configurations are not read.
- In onAboutToCreateAbility, loadHyperStartupConfig() and loadColorMode() are called.
- The onAboutToCreateAbility method is triggered in both cold startup and hyper startup. Therefore, before creating an ability, the system reads the Preferences again to ensure that the page obtains the latest saved status.
- After adaptation, the dark mode switching result will not be fixed by the old status read during the hyper startup initialization process.
Switching the Verification Version
In the entry/src/main/module.json5 file, select the AbilityStage to be used in srcEntry.
The adapted version is used by default.
{
"srcEntry": "./ets/MyAbilityStageAdapted.ets"
}
To observe the issues when the unadapted version is used, switch to:
{
"srcEntry": "./ets/MyAbilityStage.ets"
}
After the modification, recompile and run the project, and then compare the effect of switching to the dark mode in cold startup and hyper startup.
Required Permissions
No special permission is required.
Dependencies
- @kit.AbilityKit - APIs Related to AbilityStage
- @kit.ArkData - Local storage APIs for Preferences
Constraints
- Currently, the hyper startup feature is supported only on the API version 24–based HUAWEI Enjoy 90 Pro Max and HUAWEI Mate 80 Pro.
- The HarmonyOS version must be HarmonyOS 6.1.0 Release or later.
- Contact Huawei technical support to add the application to the trustlist.
- The hyper startup feature occupies a small amount of resources in the background. The number of applications that can be hyper started at the same time for each user is limited.
Introduction
本项目是展示应用快启特性接入的实例。示例页面提供快启启动开关:开启后,应用可通过快启能力完成启动;关闭后,应用将按照常规冷启动流程启动。
Customize your domain




