harmony 鸿蒙@ohos.application.AbilityLifecycleCallback (AbilityLifecycleCallback)

  • 2022-08-09
  • 浏览 (728)

@ohos.application.AbilityLifecycleCallback (AbilityLifecycleCallback)

The AbilityLifecycleCallback module provides callbacks, such as onAbilityCreate, onWindowStageCreate, and onWindowStageDestroy, to receive lifecycle state changes in the application context. These callbacks can be used as an input parameter of registerAbilityLifecycleCallback.

NOTE

The APIs of this module are supported since API version 9 and are deprecated in versions later than API version 9. You are advised to use @ohos.app.ability.AbilityLifecycleCallback instead. Newly added APIs will be marked with a superscript to indicate their earliest API version. The APIs of this module can be used only in the stage model.

Modules to Import

import AbilityLifecycleCallback from "@ohos.application.AbilityLifecycleCallback";

AbilityLifecycleCallback.onAbilityCreate

onAbilityCreate(ability: Ability): void;

Called when an ability is created.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.

AbilityLifecycleCallback.onWindowStageCreate

onWindowStageCreate(ability: Ability, windowStage: window.WindowStage): void;

Called when the window stage of an ability is created.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.
windowStage window.WindowStage Yes WindowStage object.

AbilityLifecycleCallback.onWindowStageActive

onWindowStageActive(ability: Ability, windowStage: window.WindowStage): void;

Called when the window stage of an ability gains focus.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.
windowStage window.WindowStage Yes WindowStage object.

AbilityLifecycleCallback.onWindowStageInactive

onWindowStageInactive(ability: Ability, windowStage: window.WindowStage): void;

Called when the window stage of an ability loses focus.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.
windowStage window.WindowStage Yes WindowStage object.

AbilityLifecycleCallback.onWindowStageDestroy

onWindowStageDestroy(ability: Ability, windowStage: window.WindowStage): void;

Called when the window stage of an ability is destroyed.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.
windowStage window.WindowStage Yes WindowStage object.

AbilityLifecycleCallback.onAbilityDestroy

onAbilityDestroy(ability: Ability): void;

Called when an ability is destroyed.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.

AbilityLifecycleCallback.onAbilityForeground

onAbilityForeground(ability: Ability): void;

Called when an ability is switched from the background to the foreground.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.

AbilityLifecycleCallback.onAbilityBackground

onAbilityBackground(ability: Ability): void;

Called when an ability is switched from the foreground to the background.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.

AbilityLifecycleCallback.onAbilityContinue

onAbilityContinue(ability: Ability): void;

Called when an ability is continued on another device.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
ability Ability Yes Ability object.

Example

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

var lifecycleId;

export default class MyAbilityStage extends AbilityStage {
    onCreate() {
        console.log("MyAbilityStage onCreate")
        let AbilityLifecycleCallback = {
            onAbilityCreate(ability) {
                console.log("onAbilityCreate ability:" + JSON.stringify(ability));
            },
            onWindowStageCreate(ability, windowStage) {
                console.log("onWindowStageCreate ability:" + JSON.stringify(ability));
                console.log("onWindowStageCreate windowStage:" + JSON.stringify(windowStage));
            },
            onWindowStageActive(ability, windowStage) {
                console.log("onWindowStageActive ability:" + JSON.stringify(ability));
                console.log("onWindowStageActive windowStage:" + JSON.stringify(windowStage));
            },
            onWindowStageInactive(ability, windowStage) {
                console.log("onWindowStageInactive ability:" + JSON.stringify(ability));
                console.log("onWindowStageInactive windowStage:" + JSON.stringify(windowStage));
            },
            onWindowStageDestroy(ability, windowStage) {
                console.log("onWindowStageDestroy ability:" + JSON.stringify(ability));
                console.log("onWindowStageDestroy windowStage:" + JSON.stringify(windowStage));
            },
            onAbilityDestroy(ability) {
                console.log("onAbilityDestroy ability:" + JSON.stringify(ability));
            },
            onAbilityForeground(ability) {
                console.log("onAbilityForeground ability:" + JSON.stringify(ability));
            },
            onAbilityBackground(ability) {
                console.log("onAbilityBackground ability:" + JSON.stringify(ability));
            },
            onAbilityContinue(ability) {
                console.log("onAbilityContinue ability:" + JSON.stringify(ability));
            }
        }
        // 1. Obtain applicationContext through the context attribute.
        let applicationContext = this.context.getApplicationContext();
        // 2. Use applicationContext to register a listener for the ability lifecycle in the application.
        lifecycleId = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback);
        console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleId));
    }

    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => {
            console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
        });
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

harmony 鸿蒙@ohos.WorkSchedulerExtensionAbility (Work Scheduler Callbacks)

1  赞