js-apis-app-ability-abilityLifecycleCallback.md 10.0 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.app.ability.abilityLifecycleCallback (AbilityLifecycleCallback)
D
donglin 已提交
2

D
donglin 已提交
3
AbilityLifecycleCallback模块提供应用上下文[ApplicationContext](js-apis-inner-application-applicationContext.md)的生命周期发生变化时触发相应回调的能力,包括[onAbilityCreate](#abilitylifecyclecallbackonabilitycreate)[onWindowStageCreate](#abilitylifecyclecallbackonwindowstagecreate)[onWindowStageActive](#abilitylifecyclecallbackonwindowstageactive)[onWindowStageInactive](#abilitylifecyclecallbackonwindowstageinactive)[onWindowStageDestroy](#abilitylifecyclecallbackonwindowstagedestroy)[onAbilityDestroy](#abilitylifecyclecallbackonabilitydestroy)[onAbilityForeground](#abilitylifecyclecallbackonabilityforeground)[onAbilityBackground](#abilitylifecyclecallbackonabilitybackground)[onAbilityContinue](#abilitylifecyclecallbackonabilitycontinue)方法。
D
donglin 已提交
4 5 6 7 8 9 10 11 12

> **说明:**
> 
> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
> 本模块接口仅可在Stage模型下使用。


## 导入模块

M
m00512953 已提交
13
```ts
D
donglin 已提交
14 15 16 17 18 19
import AbilityLifecycleCallback from "@ohos.app.ability.AbilityLifecycleCallback";
```


## AbilityLifecycleCallback.onAbilityCreate

D
fix  
donglin 已提交
20
onAbilityCreate(ability: UIAbility): void;
D
donglin 已提交
21 22 23 24 25 26 27 28 29

注册监听应用上下文的生命周期后,在ability创建时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
30
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 | 
D
donglin 已提交
31

D
donglin 已提交
32 33 34 35 36 37 38 39
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityCreate(ability){
        console.log("AbilityLifecycleCallback onAbilityCreate.");  
    }
}
```
D
donglin 已提交
40 41 42

## AbilityLifecycleCallback.onWindowStageCreate

D
fix  
donglin 已提交
43
onWindowStageCreate(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
44 45 46 47 48 49 50 51 52

注册监听应用上下文的生命周期后,在windowStage创建时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
53
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 |  
D
donglin 已提交
54
  | windowStage | [window.WindowStage](js-apis-window.md#windowstage9) | 是 | 当前WindowStage对象 |    
D
donglin 已提交
55

D
donglin 已提交
56 57 58 59 60 61 62 63
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageCreate(ability, windowStage){
        console.log("AbilityLifecycleCallback onWindowStageCreate.");
    }
}
```
D
donglin 已提交
64 65 66

## AbilityLifecycleCallback.onWindowStageActive

D
fix  
donglin 已提交
67
onWindowStageActive(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
68 69 70 71 72 73 74 75 76

注册监听应用上下文的生命周期后,在windowStage获焦时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
77
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 |  
D
donglin 已提交
78
  | windowStage | [window.WindowStage](js-apis-window.md#windowstage9) | 是 | 当前WindowStage对象 |    
D
donglin 已提交
79

D
donglin 已提交
80 81 82 83 84 85 86 87
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageActive(ability, windowStage){
        console.log("AbilityLifecycleCallback onWindowStageActive.");
    }
}
```
D
donglin 已提交
88 89 90

## AbilityLifecycleCallback.onWindowStageInactive

D
fix  
donglin 已提交
91
onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
92 93 94 95 96 97 98 99 100

注册监听应用上下文的生命周期后,在windowStage失焦时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
101
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 |  
D
donglin 已提交
102
  | windowStage | [window.WindowStage](js-apis-window.md#windowstage9) | 是 | 当前WindowStage对象 |  
D
donglin 已提交
103

D
donglin 已提交
104 105 106 107 108 109 110 111
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageInactive(ability, windowStage){
        console.log("AbilityLifecycleCallback onWindowStageInactive.");
    }
}
```
D
donglin 已提交
112 113 114

## AbilityLifecycleCallback.onWindowStageDestroy

D
fix  
donglin 已提交
115
onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
116 117 118 119 120 121 122 123 124

注册监听应用上下文的生命周期后,在windowStage销毁时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
125
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 |  
D
donglin 已提交
126
  | windowStage | [window.WindowStage](js-apis-window.md#windowstage9) | 是 | 当前WindowStage对象 |  
D
donglin 已提交
127

D
donglin 已提交
128 129 130 131 132 133 134 135
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageDestroy(ability, windowStage){
        console.log("AbilityLifecycleCallback onWindowStageDestroy.");
    }
}
```
D
donglin 已提交
136 137 138

## AbilityLifecycleCallback.onAbilityDestroy

D
fix  
donglin 已提交
139
onAbilityDestroy(ability: UIAbility): void;
D
donglin 已提交
140 141 142 143 144 145 146 147 148

注册监听应用上下文的生命周期后,在ability销毁时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
149
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 | 
D
donglin 已提交
150

D
donglin 已提交
151 152 153 154 155 156 157 158
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityDestroy(ability){
        console.log("AbilityLifecycleCallback onAbilityDestroy.");
    }
}
```
D
donglin 已提交
159 160 161

## AbilityLifecycleCallback.onAbilityForeground

D
fix  
donglin 已提交
162
onAbilityForeground(ability: UIAbility): void;
D
donglin 已提交
163 164 165 166 167 168 169 170 171

注册监听应用上下文的生命周期后,在ability的状态从后台转到前台时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
172
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 | 
D
donglin 已提交
173

D
donglin 已提交
174 175 176 177 178 179 180 181
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityForeground(ability){
        console.log("AbilityLifecycleCallback onAbilityForeground.");
    }
}
```
D
donglin 已提交
182 183 184

## AbilityLifecycleCallback.onAbilityBackground

D
fix  
donglin 已提交
185
onAbilityBackground(ability: UIAbility): void;
D
donglin 已提交
186 187 188 189 190 191 192 193 194

注册监听应用上下文的生命周期后,在ability的状态从前台转到后台时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
195
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 | 
D
donglin 已提交
196

D
donglin 已提交
197 198 199 200 201 202 203 204
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityBackground(ability){
        console.log("AbilityLifecycleCallback onAbilityBackground.");
    }
}
```
D
donglin 已提交
205 206 207

## AbilityLifecycleCallback.onAbilityContinue

D
fix  
donglin 已提交
208
onAbilityContinue(ability: UIAbility): void;
D
donglin 已提交
209 210 211 212 213 214 215 216 217

注册监听应用上下文的生命周期后,在ability迁移时触发回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
D
donglin 已提交
218
  | ability | [UIAbility](js-apis-app-ability-uiAbility.md) | 是 | 当前Ability对象 | 
D
donglin 已提交
219

D
donglin 已提交
220 221 222 223 224 225 226 227 228 229
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityContinue(ability){
        console.log("AbilityLifecycleCallback onAbilityContinue.");
    }
}
```
## AbilityLifecycleCallback使用

D
donglin 已提交
230
**示例:**
D
donglin 已提交
231

D
donglin 已提交
232
MyFirstAbility.ts
D
donglin 已提交
233 234
```ts
import AbilityLifecycleCallback from "@ohos.app.ability.AbilityLifecycleCallback";
D
donglin 已提交
235 236
import AbilityStage from "@ohos.app.ability.AbilityStage";
import UIAbility from '@ohos.app.ability.UIAbility';
D
donglin 已提交
237

D
donglin 已提交
238 239
// 声明ability生命周期回调,需配置所有回调后才可以在applicationContext注册
let abilityLifecycleCallback = {
D
donglin 已提交
240
    onAbilityCreate(ability){
D
donglin 已提交
241
        console.log("AbilityLifecycleCallback onAbilityCreate.");  
D
donglin 已提交
242 243
    },
    onWindowStageCreate(ability, windowStage){
D
donglin 已提交
244
        console.log("AbilityLifecycleCallback onWindowStageCreate.");
D
donglin 已提交
245 246
    },
    onWindowStageActive(ability, windowStage){
D
donglin 已提交
247
        console.log("AbilityLifecycleCallback onWindowStageActive.");
D
donglin 已提交
248 249
    },
    onWindowStageInactive(ability, windowStage){
D
donglin 已提交
250
        console.log("AbilityLifecycleCallback onWindowStageInactive.");
D
donglin 已提交
251 252
    },
    onWindowStageDestroy(ability, windowStage){
D
donglin 已提交
253
        console.log("AbilityLifecycleCallback onWindowStageDestroy.");
D
donglin 已提交
254 255
    },
    onAbilityDestroy(ability){
D
donglin 已提交
256
        console.log("AbilityLifecycleCallback onAbilityDestroy.");
D
donglin 已提交
257 258
    },
    onAbilityForeground(ability){
D
donglin 已提交
259
        console.log("AbilityLifecycleCallback onAbilityForeground.");
D
donglin 已提交
260 261
    },
    onAbilityBackground(ability){
D
donglin 已提交
262
        console.log("AbilityLifecycleCallback onAbilityBackground.");
D
donglin 已提交
263 264
    },
    onAbilityContinue(ability){
D
donglin 已提交
265
        console.log("AbilityLifecycleCallback onAbilityContinue.");
D
donglin 已提交
266 267 268
    }
}

D
donglin 已提交
269
export default class MyFirstAbility extends UIAbility {
D
donglin 已提交
270
    onCreate() {
D
donglin 已提交
271
        console.log("MyAbilityStage onCreate");
D
donglin 已提交
272 273 274
        // 1.通过context属性获取applicationContext
        let applicationContext = this.context.getApplicationContext();
        // 2.通过applicationContext注册监听应用内生命周期
275
        try {
D
donglin 已提交
276
            globalThis.lifecycleId = applicationContext.on("abilityLifecycle", abilityLifecycleCallback);
277 278 279 280
            console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleId));
        } catch (paramError) {
            console.log("error: " + paramError.code + " ," + paramError.message);
        }
D
donglin 已提交
281 282 283 284
    }
}
```

D
donglin 已提交
285
MySecondAbility.ts
D
donglin 已提交
286 287 288
```ts
import UIAbility from "ohos.app.ability.UIAbility"

D
donglin 已提交
289
export default class MySecondAbility extends UIAbility {
D
donglin 已提交
290
    onDestroy() {
D
donglin 已提交
291 292
        let applicationContext = this.context.getApplicationContext();
        // 3.通过applicationContext注销监听应用内生命周期
D
donglin 已提交
293
        applicationContext.off("abilityLifecycle", globalThis.lifecycleId, (error) => {
294 295
            if (error.code != 0) {
                console.log("unregisterAbilityLifecycleCallback failed, error: " + JSON.stringify(error));
D
donglin 已提交
296 297 298 299 300
            } else {
                console.log("unregisterAbilityLifecycleCallback success.");
            }
        });
    }
D
donglin 已提交
301 302
}
```