harmony 鸿蒙AbilityStage Component Container

  • 2023-02-03
  • 浏览 (778)

AbilityStage Component Container

AbilityStage is a component container at the module level. When the HAP of an application is loaded for the first time, an AbilityStage instance is created. You can perform operations such as initialization on the instance.

An AbilityStage instance corresponds to a module.

AbilityStage is not automatically generated in the default project of DevEco Studio. To use AbilityStage, you can manually create an AbilityStage file. The procedure is as follows:

  1. In the ets directory of the Module project, right-click and choose New > Directory to create a directory named myabilitystage.

  2. In the myabilitystage directory, right-click and choose New > TypeScript File to create a file named MyAbilityStage.ts.

  3. Open the MyAbilityStage.ts file, and import the dependency package of AbilityStage. Customize a class that inherits from AbilityStage, and add the required lifecycle callbacks. The following code snippet adds the onCreate() lifecycle callback.

   import AbilityStage from '@ohos.app.ability.AbilityStage';
   import Want from '@ohos.app.ability.Want';
   
   export default class MyAbilityStage extends AbilityStage {
     onCreate() {
       // When the HAP of the application is loaded for the first time, initialize the module.
     }
     onAcceptWant(want: Want) {
       // Triggered only for the UIAbility with the specified launch type.
       return "MyAbilityStage";
     }
   }
  1. In the module.json5 file, set srcEntry to specify the code path of the module as the entry for loading the HAP. json { "module": { "name": "entry", "type": "entry", "srcEntry": "./ets/myabilitystage/MyAbilityStage.ts", ... } }

AbilityStage has the lifecycle callback onCreate() and the event callbacks onAcceptWant(), onConfigurationUpdated(), and onMemoryLevel().

  • onCreate() lifecycle callback: Before the first UIAbility instance of a module is loaded, an AbilityStage instance is created. This callback is invoked when the AbilityStage instance is created. The AbilityStage module notifies you of when you can perform module initialization such as resource pre-loading and thread creation during module loading.

  • onAcceptWant() event callback: triggered when the UIAbility is started in specified mode. For details, see UIAbility Component Launch Type.

  • onConfigurationUpdated() event callback: triggered when the global system configuration changes. The global system configuration, such as the system language and theme, are defined in the Configuration class before project configuration.

  • onMemoryLevel() event callback: triggered when the system adjusts the memory.

When an application is switched to the background, it is cached in the background. This adversely affects the overall system performance. When system resources are insufficient, the system reclaims memory from applications in multiple ways. For example, the system may stop applications to release memory for executing key tasks. To further maintain the balance of the system memory and prevent the system from stopping application processes, you can subscribe to the system memory changes in the onMemoryLevel() lifecycle callback of AbilityStage to release unnecessary resources.

  import AbilityStage from '@ohos.app.ability.AbilityStage';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  
  export default class MyAbilityStage extends AbilityStage {
    onMemoryLevel(level: AbilityConstant.MemoryLevel) {
      // Release unnecessary memory based on the change of available system memory.
    }
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

harmony 鸿蒙Application- or Component-Level Configuration (FA Model)

0  赞