• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AbilityForegroundStateObserver (System API)
2
3The module defines the listener used to listen for ability foreground and background state changes.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> The APIs provided by this module are system APIs.
9
10## Modules to Import
11
12```ts
13import { abilityManager } from '@kit.AbilityKit';
14```
15
16## AbilityForegroundStateObserver
17
18### onAbilityStateChanged
19
20onAbilityStateChanged(abilityStateData: AbilityStateData): void
21
22Called when the ability is switched between foreground and background.
23
24**System capability**: SystemCapability.Ability.AbilityRuntime.Core
25
26**System API**: This is a system API.
27
28**Parameters**
29
30| Name | Type | Mandatory | Description |
31| ------ | ----- | ----- | ----- |
32| abilityStateData   | [AbilityStateData](js-apis-inner-application-abilityStateData.md)   | Yes| Ability state data.|
33
34**Example**
35```ts
36import { abilityManager } from '@kit.AbilityKit';
37import { BusinessError } from '@kit.BasicServicesKit';
38
39let observer: abilityManager.AbilityForegroundStateObserver = {
40  onAbilityStateChanged(abilityStateData) {
41    console.info(`onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
42  },
43};
44try {
45  abilityManager.on('abilityForegroundState', observer);
46} catch (paramError) {
47  let code = (paramError as BusinessError).code;
48  let message = (paramError as BusinessError).message;
49  console.error(`error code: ${code}, error msg: ${message}`);
50}
51```
52