• 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> **NOTE**
6>
7> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import appManager from '@ohos.app.ability.appManager';
13```
14
15**System capability**: SystemCapability.Ability.AbilityRuntime.Core
16
17**System API**: This is a system API and cannot be called by third-party applications.
18
19| Name                    |                      | Type    | Readable| Writable| Description                      |
20| ----------------------- | ---------| ---- | ---- | ------------------------- | ------------------------- |
21| onForegroundApplicationChanged         | [AppStateData](js-apis-inner-application-appStateData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the foreground or background state of an application changes.                   |
22| onAbilityStateChanged  | [AbilityStateData](js-apis-inner-application-abilityStateData.md) | AsyncCallback\<void>   | Yes  | No | Callback invoked when the ability state changes.                 |
23| onProcessCreated        | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is created.                 |
24| onProcessDied         | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is destroyed.                 |
25| onProcessStateChanged<sup>9+</sup>         | [ProcessData](js-apis-inner-application-processData.md) | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the process state is changed.                 |
26
27**Example**
28```ts
29let applicationStateObserver = {
30    onForegroundApplicationChanged(appStateData) {
31        console.log('onForegroundApplicationChanged appStateData: ' +  JSON.stringify(appStateData));
32    },
33    onAbilityStateChanged(abilityStateData) {
34        console.log('onAbilityStateChanged onAbilityStateChanged: ' +  JSON.stringify(abilityStateData));
35    },
36    onProcessCreated(processData) {
37        console.log('onProcessCreated onProcessCreated: '  +  JSON.stringify(processData));
38    },
39    onProcessDied(processData) {
40        console.log('onProcessDied onProcessDied: '  +  JSON.stringify(processData));
41    },
42    onProcessStateChanged(processData) {
43        console.log('onProcessStateChanged onProcessStateChanged: ' +  JSON.stringify(processData));
44    }
45};
46let observerCode = appManager.registerApplicationStateObserver(applicationStateObserver);
47```
48