1# AbilityForegroundStateObserver (System API)
2
3The **AbilityForegroundStateObserver** 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## Attributes
17
18**System capability**: SystemCapability.Ability.AbilityRuntime.Core
19
20| Name                            | Type                   | Readable| Writable| Description  |
21| -------------------------------- | ---------------------- | ---- | ---- | ------------------ |
22| onAbilityStateChanged   | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the foreground or background state of an ability changes. The parameter type passed in is [AbilityStateData](js-apis-inner-application-abilityStateData.md).|
23
24**Example**
25```ts
26import { abilityManager } from '@kit.AbilityKit';
27import { BusinessError } from '@kit.BasicServicesKit';
28
29let observer: abilityManager.AbilityForegroundStateObserver = {
30  onAbilityStateChanged(abilityStateData) {
31    console.log(`onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
32  },
33};
34try {
35  abilityManager.on('abilityForegroundState', observer);
36} catch (paramError) {
37  let code = (paramError as BusinessError).code;
38  let message = (paramError as BusinessError).message;
39  console.error(`error code: ${code}, error msg: ${message}`);
40}
41```
42