js-apis-app-ability-abilityLifecycleCallback.md 11.8 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

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

## 导入模块

M
m00512953 已提交
12
```ts
M
mingxihua 已提交
13
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
D
donglin 已提交
14 15 16 17
```

## AbilityLifecycleCallback.onAbilityCreate

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

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

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

**参数:**

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

D
donglin 已提交
30 31
**示例:**
```ts
L
liuliu 已提交
32 33 34
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
35
    onAbilityCreate(ability){
M
mingxihua 已提交
36
        console.log('AbilityLifecycleCallback onAbilityCreate.');  
D
donglin 已提交
37
    }
38
};
D
donglin 已提交
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
**示例:**
```ts
L
liuliu 已提交
58 59 60
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
61
    onWindowStageCreate(ability, windowStage){
M
mingxihua 已提交
62
        console.log('AbilityLifecycleCallback onWindowStageCreate.');
D
donglin 已提交
63
    }
64
};
D
donglin 已提交
65
```
D
donglin 已提交
66 67 68

## AbilityLifecycleCallback.onWindowStageActive

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

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

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

**参数:**

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

D
donglin 已提交
82 83
**示例:**
```ts
L
liuliu 已提交
84 85 86
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
87
    onWindowStageActive(ability, windowStage){
M
mingxihua 已提交
88
        console.log('AbilityLifecycleCallback onWindowStageActive.');
D
donglin 已提交
89
    }
90
};
D
donglin 已提交
91
```
D
donglin 已提交
92 93 94

## AbilityLifecycleCallback.onWindowStageInactive

D
fix  
donglin 已提交
95
onWindowStageInactive(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
96 97 98 99 100 101 102 103 104

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

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

**参数:**

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

D
donglin 已提交
108 109
**示例:**
```ts
L
liuliu 已提交
110 111 112
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
113
    onWindowStageInactive(ability, windowStage){
M
mingxihua 已提交
114
        console.log('AbilityLifecycleCallback onWindowStageInactive.');
D
donglin 已提交
115
    }
116
};
D
donglin 已提交
117
```
D
donglin 已提交
118 119 120

## AbilityLifecycleCallback.onWindowStageDestroy

D
fix  
donglin 已提交
121
onWindowStageDestroy(ability: UIAbility, windowStage: window.WindowStage): void;
D
donglin 已提交
122 123 124 125 126 127 128 129 130

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

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

**参数:**

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

D
donglin 已提交
134 135
**示例:**
```ts
L
liuliu 已提交
136 137 138
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
139
    onWindowStageDestroy(ability, windowStage){
M
mingxihua 已提交
140
        console.log('AbilityLifecycleCallback onWindowStageDestroy.');
D
donglin 已提交
141
    }
142
};
D
donglin 已提交
143
```
D
donglin 已提交
144 145 146

## AbilityLifecycleCallback.onAbilityDestroy

D
fix  
donglin 已提交
147
onAbilityDestroy(ability: UIAbility): void;
D
donglin 已提交
148 149 150 151 152 153 154 155 156

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

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

**参数:**

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

D
donglin 已提交
159 160
**示例:**
```ts
L
liuliu 已提交
161 162 163
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
164
    onAbilityDestroy(ability){
M
mingxihua 已提交
165
        console.log('AbilityLifecycleCallback onAbilityDestroy.');
D
donglin 已提交
166
    }
167
};
D
donglin 已提交
168
```
D
donglin 已提交
169 170 171

## AbilityLifecycleCallback.onAbilityForeground

D
fix  
donglin 已提交
172
onAbilityForeground(ability: UIAbility): void;
D
donglin 已提交
173 174 175 176 177 178 179 180 181

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

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

**参数:**

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

D
donglin 已提交
184 185
**示例:**
```ts
L
liuliu 已提交
186 187 188
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
189
    onAbilityForeground(ability){
M
mingxihua 已提交
190
        console.log('AbilityLifecycleCallback onAbilityForeground.');
D
donglin 已提交
191
    }
192
};
D
donglin 已提交
193
```
D
donglin 已提交
194 195 196

## AbilityLifecycleCallback.onAbilityBackground

D
fix  
donglin 已提交
197
onAbilityBackground(ability: UIAbility): void;
D
donglin 已提交
198 199 200 201 202 203 204 205 206

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

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

**参数:**

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

D
donglin 已提交
209 210
**示例:**
```ts
L
liuliu 已提交
211 212 213
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
214
    onAbilityBackground(ability){
M
mingxihua 已提交
215
        console.log('AbilityLifecycleCallback onAbilityBackground.');
D
donglin 已提交
216
    }
217
};
D
donglin 已提交
218
```
D
donglin 已提交
219 220 221

## AbilityLifecycleCallback.onAbilityContinue

D
fix  
donglin 已提交
222
onAbilityContinue(ability: UIAbility): void;
D
donglin 已提交
223 224 225 226 227 228 229 230 231

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

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

**参数:**

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

D
donglin 已提交
234 235
**示例:**
```ts
L
liuliu 已提交
236 237 238
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';

let abilityLifecycleCallback: AbilityLifecycleCallback =  {
D
donglin 已提交
239
    onAbilityContinue(ability){
M
mingxihua 已提交
240
        console.log('AbilityLifecycleCallback onAbilityContinue.');
D
donglin 已提交
241
    }
242
};
D
donglin 已提交
243
```
244

D
donglin 已提交
245 246
## AbilityLifecycleCallback使用

D
donglin 已提交
247
**示例:**
L
liuliu 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
GlobalContext.ts
```ts
// 构造单例对象
export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object | undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
```
D
donglin 已提交
272

D
donglin 已提交
273
MyFirstAbility.ts
D
donglin 已提交
274
```ts
M
mingxihua 已提交
275 276
import AbilityLifecycleCallback from '@ohos.app.ability.AbilityLifecycleCallback';
import AbilityStage from '@ohos.app.ability.AbilityStage';
D
donglin 已提交
277
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
278
import { GlobalContext } from '../GlobalContext'
D
donglin 已提交
279

D
donglin 已提交
280
// 声明ability生命周期回调,需配置所有回调后才可以在applicationContext注册
L
liuliu 已提交
281
let abilityLifecycleCallback: AbilityLifecycleCallback = {
D
donglin 已提交
282
    onAbilityCreate(ability){
M
mingxihua 已提交
283
        console.log('AbilityLifecycleCallback onAbilityCreate.');  
D
donglin 已提交
284 285
    },
    onWindowStageCreate(ability, windowStage){
M
mingxihua 已提交
286
        console.log('AbilityLifecycleCallback onWindowStageCreate.');
D
donglin 已提交
287 288
    },
    onWindowStageActive(ability, windowStage){
M
mingxihua 已提交
289
        console.log('AbilityLifecycleCallback onWindowStageActive.');
D
donglin 已提交
290 291
    },
    onWindowStageInactive(ability, windowStage){
M
mingxihua 已提交
292
        console.log('AbilityLifecycleCallback onWindowStageInactive.');
D
donglin 已提交
293 294
    },
    onWindowStageDestroy(ability, windowStage){
M
mingxihua 已提交
295
        console.log('AbilityLifecycleCallback onWindowStageDestroy.');
D
donglin 已提交
296 297
    },
    onAbilityDestroy(ability){
M
mingxihua 已提交
298
        console.log('AbilityLifecycleCallback onAbilityDestroy.');
D
donglin 已提交
299 300
    },
    onAbilityForeground(ability){
M
mingxihua 已提交
301
        console.log('AbilityLifecycleCallback onAbilityForeground.');
D
donglin 已提交
302 303
    },
    onAbilityBackground(ability){
M
mingxihua 已提交
304
        console.log('AbilityLifecycleCallback onAbilityBackground.');
D
donglin 已提交
305 306
    },
    onAbilityContinue(ability){
M
mingxihua 已提交
307
        console.log('AbilityLifecycleCallback onAbilityContinue.');
D
donglin 已提交
308
    }
309
};
D
donglin 已提交
310

D
donglin 已提交
311
export default class MyFirstAbility extends UIAbility {
D
donglin 已提交
312
    onCreate() {
M
mingxihua 已提交
313
        console.log('MyAbilityStage onCreate');
D
donglin 已提交
314 315 316
        // 1.通过context属性获取applicationContext
        let applicationContext = this.context.getApplicationContext();
        // 2.通过applicationContext注册监听应用内生命周期
317
        try {
L
liuliu 已提交
318 319
            let lifecycleId = applicationContext.on('abilityLifecycle', abilityLifecycleCallback);
            GlobalContext.getContext().setObject("lifecycleId", lifecycleId);
M
mingxihua 已提交
320
            console.log('registerAbilityLifecycleCallback lifecycleId: ${globalThis.lifecycleId}');
321
        } catch (paramError) {
M
mingxihua 已提交
322
            console.error('error: ${paramError.code}, ${paramError.message}');
323
        }
D
donglin 已提交
324 325 326 327
    }
}
```

D
donglin 已提交
328
MySecondAbility.ts
D
donglin 已提交
329
```ts
M
mingxihua 已提交
330
import UIAbility from '@ohos.app.ability.UIAbility';
L
liuliu 已提交
331
import { GlobalContext } from '../GlobalContext'
D
donglin 已提交
332

D
donglin 已提交
333
export default class MySecondAbility extends UIAbility {
D
donglin 已提交
334
    onDestroy() {
D
donglin 已提交
335
        let applicationContext = this.context.getApplicationContext();
L
liuliu 已提交
336
        let lifecycleId = GlobalContext.getContext().getObject("lifecycleId") as number;
D
donglin 已提交
337
        // 3.通过applicationContext注销监听应用内生命周期
L
liuliu 已提交
338
        applicationContext.off('abilityLifecycle', lifecycleId, (error) => {
D
donglin 已提交
339
            if (error && error.code !== 0) {
M
mingxihua 已提交
340
                console.error('unregisterAbilityLifecycleCallback fail, error: ${JSON.stringify(error)}');
D
donglin 已提交
341
            } else {
M
mingxihua 已提交
342
                console.log('unregisterAbilityLifecycleCallback success.');
D
donglin 已提交
343 344 345
            }
        });
    }
D
donglin 已提交
346 347
}
```