1# Application Preloading 2 3## Overview 4 5Starting from API version 20, the application preloading feature is provided. This feature preloads applications to a specific stage when system resources are sufficient, based on user usage patterns. When a user launches an application, the startup time is reduced because part of the application has already been loaded, which helps enhance user experience and the competitiveness of the application. 6 7This feature is particularly suitable for applications that take a long time to start due to the loading of large amounts of resources, such as large gaming applications and large office applications. 8 9## Constraints 10 11- Currently, this feature is supported only on 2-in-1 devices. 12 13- Preloading is supported only for the AbilityStage and UIAbility of the entry module. Regardless of the preloading stage, the entry module must be configured with an entry UIAbility. For details, see step 2 in [How to Develop](#how-to-develop). 14 15- After an application is configured for preloading, whether preloading actually occurs and the specific timing of preloading are determined by the system based on user habits and other information. You cannot intervene in this process. 16 17## Working Principles 18 19When system resources are sufficient, the system preloads the application to a specific stage to improve startup speed. Currently, three preloading stages are supported. You can choose one based on the time-consuming stages of the application's cold start. 20 21> **NOTE** 22> 23> During application preloading, no UI is displayed. Therefore, no operations related to UI display, interaction, or user visibility should be included at any preloading stage. It is also essential to ensure that all functions operate normally and user experience is not affected when the user officially launches the application. 24 25- **processCreated**: preloads the application to the stage where the process is created. When this stage is configured, the preloading feature creates an empty process and initializes the application, but no lifecycle callbacks are triggered. 26 27- **abilityStageCreated**: preloads the application to the stage where the [AbilityStage](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md) is created. When this stage is configured, the preloading feature creates an empty process and initializes the application, and then triggers the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#oncreate) callback of the [AbilityStage](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md) of the entry module. 28 29- **windowStageCreated**: preloads the application to the stage where the [WindowStage](../reference/apis-arkui/arkts-apis-window-WindowStage.md) is created. When this stage is configured, the preloading feature creates an empty process and initializes the application, and then triggers the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#oncreate) callback of the [AbilityStage](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md) of the entry module. It then launches the entry UIAbility of the entry module and triggers its [onCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#oncreate) and [onWindowStageCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#onwindowstagecreate) callbacks. You can obtain the launch reason in the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#oncreate) callback of the UIAbility through the enum [launchParam.launchReason](../reference/apis-ability-kit/js-apis-app-ability-abilityConstant.md#launchreason). The enumerated value **PRELOAD** indicates that the current UIAbility is launched by preloading. 30 31 32 33## How to Develop 34 351. Declare the preloading stage supported by the application. 36 37 For example, to preload the application to the **windowStageCreated** stage, configure the [appPreloadPhase](../quick-start/app-configuration-file.md#tags-in-the-configuration-file) tag in the [app.json5](../quick-start/app-configuration-file.md) file. 38 39 ```json 40 { 41 "app": { 42 "bundleName": "com.demo.preloadtest", 43 "vendor": "example", 44 "versionCode": 1000000, 45 "versionName": "1.0.0", 46 "icon": "$media:layered_image", 47 "label": "$string:app_name", 48 "appPreloadPhase": "windowStageCreated" 49 } 50 } 51 ``` 52 532. Configure the entry UIAbility (automatically configured by default when you create a project). 54 55 For example, for an EntryAbility, in the [module.json5](../quick-start/module-configuration-file.md) file of the entry module, set **mainElement** to **EntryAbility**, and add **"entity.system.home"** to **entities** and **"ohos.want.action.home"** to **actions** in the **skills** tag of **EntryAbility**. 56 57 ```json 58 { 59 "module": { 60 "name": "entry", 61 "type": "entry", 62 "description": "$string:module_desc", 63 "mainElement": "EntryAbility", 64 "deviceTypes": [ 65 "2in1" 66 ], 67 "deliveryWithInstall": true, 68 "installationFree": false, 69 "pages": "$profile:main_pages", 70 "abilities": [ 71 { 72 "name": "EntryAbility", 73 "srcEntry": "./ets/entryability/EntryAbility.ets", 74 "description": "$string:EntryAbility_desc", 75 "icon": "$media:layered_image", 76 "label": "$string:EntryAbility_label", 77 "startWindowIcon": "$media:startIcon", 78 "startWindowBackground": "$color:start_window_background", 79 "exported": true, 80 "skills": [ 81 { 82 "entities": [ 83 "entity.system.home" 84 ], 85 "actions": [ 86 "ohos.want.action.home" 87 ] 88 } 89 ] 90 } 91 ] 92 } 93 } 94 ``` 95 963. (Optional) Obtain the launch reason of the UIAbility. 97 98 When **appPreloadPhase** is set to **windowStageCreated**, you can obtain the launch reason in the [onCreate](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#oncreate) lifecycle callback of the UIAbility through the enumerated value of [launchParam.launchReason](../reference/apis-ability-kit/js-apis-app-ability-abilityConstant.md#launchreason). The enumerated value **PRELOAD** indicates that the current UIAbility is launched by preloading. 99 100 ```ts 101 import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 102 103 export default class EntryAbility extends UIAbility { 104 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 105 console.info(`EntryAbility onCreate, LaunchReason:${launchParam.launchReason}`); 106 // Check whether the UIAbility is started by preloading. 107 let isPreloadStart = launchParam.launchReason === AbilityConstant.LaunchReason.PRELOAD; 108 // ... 109 } 110 } 111 ``` 112