目录

Application Preloading

Overview

Starting 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.

This 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.

Constraints

Working Principles

When 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.

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.

How to Develop

  1. Declare the preloading stage supported by the application.

    For example, to preload the application to the windowStageCreated stage, configure the appPreloadPhase tag in the app.json5 file.

    {
      "app": {
        "bundleName": "com.demo.preloadtest",
        "vendor": "example",
        "versionCode": 1000000,
        "versionName": "1.0.0",
        "icon": "$media:layered_image",
        "label": "$string:app_name",
        "appPreloadPhase": "windowStageCreated"
      }
    }
  2. Configure the entry UIAbility (automatically configured by default when you create a project).

    1. For example, for an EntryAbility, in the module.json5 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.

    2. If appPreloadPhase in the app.json5 file is set to windowStageCreated, set launchType of the EntryAbility in the entry module's module.json5 file to singleton or specified.

    {
      "module": {
        "name": "entry",
        "type": "entry",
        "mainElement": "EntryAbility",
        // ...
        "abilities": [
          {
            "name": "EntryAbility",
            "srcEntry": "./ets/entryability/EntryAbility.ets",
            "launchType": "singleton",
            "skills": [
              {
                "entities": [
                  "entity.system.home"
                ],
                "actions": [
                  "ohos.want.action.home"
                ]
              }
            ]
            // ...
          }
        ]
      }
    }
  3. (Optional) Obtain the launch reason of the UIAbility.

    When appPreloadPhase is set to windowStageCreated, you can obtain the launch reason in the onCreate lifecycle callback of the UIAbility through the enumerated value of launchParam.launchReason. The enumerated value PRELOAD indicates that the current UIAbility is launched by preloading.

    import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
    
    export default class EntryAbility extends UIAbility {
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
        console.info(`EntryAbility onCreate, LaunchReason:${launchParam.launchReason}`);
        // Check whether the UIAbility is started by preloading.
        let isPreloadStart = launchParam.launchReason === AbilityConstant.LaunchReason.PRELOAD;
        // ...
      }
    }