• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AbilityForegroundStateObserver
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 '@ohos.app.ability.abilityManager';
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-appStateData.md).|
23
24**Example**
25```ts
26import abilityManager from '@ohos.app.ability.abilityManager';
27import { BusinessError } from '@ohos.base';
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}, ${message} `);
40}
41```
42