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
M
mingxihua 已提交
14
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
D
donglin 已提交
15 16 17 18 19
```


## 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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityCreate(ability){
M
mingxihua 已提交
36
        console.log('AbilityLifecycleCallback onAbilityCreate.');  
D
donglin 已提交
37 38 39
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageCreate(ability, windowStage){
M
mingxihua 已提交
60
        console.log('AbilityLifecycleCallback onWindowStageCreate.');
D
donglin 已提交
61 62 63
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageActive(ability, windowStage){
M
mingxihua 已提交
84
        console.log('AbilityLifecycleCallback onWindowStageActive.');
D
donglin 已提交
85 86 87
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageInactive(ability, windowStage){
M
mingxihua 已提交
108
        console.log('AbilityLifecycleCallback onWindowStageInactive.');
D
donglin 已提交
109 110 111
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onWindowStageDestroy(ability, windowStage){
M
mingxihua 已提交
132
        console.log('AbilityLifecycleCallback onWindowStageDestroy.');
D
donglin 已提交
133 134 135
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityDestroy(ability){
M
mingxihua 已提交
155
        console.log('AbilityLifecycleCallback onAbilityDestroy.');
D
donglin 已提交
156 157 158
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityForeground(ability){
M
mingxihua 已提交
178
        console.log('AbilityLifecycleCallback onAbilityForeground.');
D
donglin 已提交
179 180 181
    }
};
```
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
**示例:**
```ts
let abilityLifecycleCallback =  {
    onAbilityBackground(ability){
M
mingxihua 已提交
201
        console.log('AbilityLifecycleCallback onAbilityBackground.');
D
donglin 已提交
202 203 204
    }
};
```
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 220

**示例:**
D
donglin 已提交
221 222 223
```ts
let abilityLifecycleCallback =  {
    onAbilityContinue(ability){
M
mingxihua 已提交
224
        console.log('AbilityLifecycleCallback onAbilityContinue.');
D
donglin 已提交
225 226 227 228 229 230 231 232 233 234
    }
};
```

## AbilityLifecycleCallback使用

**示例:**

MyFirstAbility.ts
```ts
M
mingxihua 已提交
235 236
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
import AbilityStage from '@ohos.app.ability.AbilityStage';
D
donglin 已提交
237 238 239 240 241
import UIAbility from '@ohos.app.ability.UIAbility';

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

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

MySecondAbility.ts
```ts
M
mingxihua 已提交
288
import UIAbility from 'ohos.app.ability.UIAbility';
D
donglin 已提交
289 290 291 292 293

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