• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ApplicationStateObserver
2
3The **ApplicationStateObserver** module defines an observer to listen for application state changes. It can be used as an input parameter in [registerApplicationStateObserver](js-apis-application-appManager.md#appmanagerregisterapplicationstateobserver8) to listen for lifecycle changes of the current application.
4
5**System capability**: SystemCapability.Ability.AbilityRuntime.Core
6
7**System API**: This is a system API and cannot be called by third-party applications.
8
9| Name                    |                      | Type    | Readable| Writable| Description                      |
10| ----------------------- | ---------| ---- | ---- | ------------------------- | ------------------------- |
11| onForegroundApplicationChanged<sup>8+</sup>         | [AppStateData](js-apis-inner-application-appStateData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the foreground or background state of an application changes.                   |
12| onAbilityStateChanged<sup>8+</sup>  | [AbilityStateData](js-apis-inner-application-abilityStateData.md) | AsyncCallback\<void>   | Yes  | No | Callback invoked when the ability state changes.                 |
13| onProcessCreated<sup>8+</sup>         | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is created.                 |
14| onProcessDied<sup>8+</sup>         | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is destroyed.                 |
15| onProcessStateChanged<sup>8+</sup>         | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the process state is changed.                 |
16
17**Example**
18```ts
19import appManager from "@ohos.application.appManager";
20
21let applicationStateObserver = {
22    onForegroundApplicationChanged(appStateData) {
23        console.log('onForegroundApplicationChanged appStateData: ' +  JSON.stringify(appStateData));
24    },
25    onAbilityStateChanged(abilityStateData) {
26        console.log('onAbilityStateChanged onAbilityStateChanged: ' +  JSON.stringify(abilityStateData));
27    },
28    onProcessCreated(processData) {
29        console.log('onProcessCreated onProcessCreated: '  +  JSON.stringify(processData));
30    },
31    onProcessDied(processData) {
32        console.log('onProcessDied onProcessDied: '  +  JSON.stringify(processData));
33    },
34    onProcessStateChanged(processData) {
35        console.log('onProcessStateChanged onProcessStateChanged: ' +  JSON.stringify(processData));
36    }
37};
38let observerCode = appManager.registerApplicationStateObserver(applicationStateObserver);
39```
40