HarmonyOS/OpenHarmony (Stage model) card development AbilityStage component container

AbilityStage is a Module-level component container. The application's HAP will create an AbilityStage instance when it is loaded for the first time, and the Module can be initialized and other operations.
AbilityStage corresponds to Module one-to-one, that is, a Module has one AbilityStage.
AbilityStage is not automatically generated in the default project of DevEco Studio. If you need to use the ability of AbilityStage, you can manually create a new AbilityStage file. The specific steps are as follows.

1. In the ets directory corresponding to the project Module, right-click and select "New > Directory" to create a new directory and name it myabilitystage.
2. In the myabilitystage directory, right-click and select "New > TypeScript File" to create a new TypeScript file and name it MyAbilityStage.ts.
3. Open the MyAbilityStage.ts file, import the dependency package of AbilityStage, and customize the class to inherit AbilityStage and add the required life cycle callbacks. In the example, an onCreate() life cycle callback is added.

import AbilityStage from '@ohos.app.ability.AbilityStage';

export default class MyAbilityStage extends AbilityStage {
  onCreate() {
    // 应用的HAP在首次加载的时,为该Module初始化操作
  }
  onAcceptWant(want) {
    // 仅specified模式下触发
    return "MyAbilityStage";
  }
}

4. In the module.json5 configuration file, specify the code path corresponding to the module by configuring the srcEntry parameter as the entry point for HAP loading.

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

AbilityStage has onCreate() life cycle callbacks and onAcceptWant(), onConfigurationUpdated(), onMemoryLevel() event callbacks.

onCreate() life cycle callback: AbilityStage will be created before loading the first UIAbility instance of the corresponding Module, and its onCreate() life cycle callback will be executed after the AbilityStage is created. The AbilityStage module provides the ability to notify developers when a Module is loaded, so that the Module can be initialized (such as resource preloading, thread creation, etc.).

onAcceptWant() event callback: event callback triggered when UIAbility specified instance mode (specified) is started. For specific usage, please see UIAbility startup mode overview.

onConfigurationUpdated() event callback: an event triggered when the global system configuration changes. System language, dark and light colors, etc. are all defined in the Configuration class before configuration items.

onMemoryLevel() event callback: event triggered when the system adjusts memory.
When an app is switched to the background, the system will keep the app in the background in the cache. Even if the application is in cache, it will affect the overall system performance. When system resources are insufficient, the system will reclaim memory from applications in a variety of ways and, if necessary, stop the application completely to free up memory for critical tasks. In order to further maintain the balance of system memory and prevent the system from stopping the user's application process, you can subscribe to changes in system memory in the onMemoryLevel() life cycle callback in AbilityStage and release unnecessary resources.

import AbilityStage from '@ohos.app.ability.AbilityStage';

export default class MyAbilityStage extends AbilityStage {
    onMemoryLevel(level) {
        // 根据系统可用内存的变化情况,释放不必要的内存
    }
}

Guess you like

Origin blog.csdn.net/weixin_69135651/article/details/132457607