目录

AbilityStage Component Manager

Overview

AbilityStage is a Module-level component manager. When the application HAP is loaded for the first time, an AbilityStage instance is created, which can be used to initialize the Module. AbilityStage corresponds to HAP on a one-to-one basis, that is, each HAP has one AbilityStage instance.

AbilityStage has the lifecycle callbacks onCreate() and onDestroy() and the event callbacks onAcceptWant(), onConfigurationUpdate(), onMemoryLevel(), onNewProcessRequest(), and onPrepareTermination().

How to Develop

[h2]Creating an AbilityStage File

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

  2. In the exampleabilitystage directory, right-click and choose New > ArkTS File to create a file named MyAbilityStage.ets.

  3. Open the MyAbilityStage.ets 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, Want } from '@kit.AbilityKit';
    
    export default class MyAbilityStage extends AbilityStage {
      onCreate(): void {
        // This callback is triggered when the HAP is loaded for the first time. In this callback, you can initialize the module (for example, pre-load resources and create threads).
      }
    
      onAcceptWant(want: Want): string {
        // Triggered only for the UIAbility with the specified launch type.
        return 'MyAbilityStage';
      }
    }
  4. In the module.json5 file, set srcEntry to specify the code path of the module as the entry for loading the HAP.

    {
      "module": {
        "name": "entry",
        "type": "entry",
        "srcEntry": "./ets/myabilitystage/MyAbilityStage.ets",
        // ···
      }
    }

[h2]Listening for System Environment Variable Changes

Here is an example of how to use the callback functions of the AbilityStage component to track changes in system environment variables.