• 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## Attributes
16
17**System capability**: SystemCapability.Ability.AbilityRuntime.Core
18
19**System API**: This is a system API and cannot be called by third-party applications.
20
21| Name                            | Type                   | Readable| Writable| Description  |
22| -------------------------------- | ---------------------- | ---- | ---- | ------------------ |
23| onForegroundApplicationChanged   | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the foreground or background state of an application changes. The parameter type passed in is [AppStateData](js-apis-inner-application-appStateData.md).|
24| onAbilityStateChanged            | AsyncCallback\<void>   | Yes  | No | Callback invoked when the ability state changes. The parameter type passed in is [AppStateData](js-apis-inner-application-appStateData.md).  |
25| onProcessCreated                 | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is created. The parameter type passed in is [ProcessData](js-apis-inner-application-processData.md).         |
26| onProcessDied                     | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is destroyed. The parameter type passed in is [ProcessData](js-apis-inner-application-processData.md).         |
27| onProcessStateChanged<sup>9+</sup> | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the process state is changed. The parameter type passed in is [ProcessData](js-apis-inner-application-processData.md).       |
28
29**Example**
30```ts
31import appManager from '@ohos.app.ability.appManager';
32
33let applicationStateObserver: appManager.ApplicationStateObserver = {
34    onForegroundApplicationChanged(appStateData) {
35        console.log(`onForegroundApplicationChanged appStateData: ${JSON.stringify(appStateData)}`);
36    },
37    onAbilityStateChanged(abilityStateData) {
38        console.log(`onAbilityStateChanged onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
39    },
40    onProcessCreated(processData) {
41        console.log(`onProcessCreated onProcessCreated: ${JSON.stringify(processData)}`);
42    },
43    onProcessDied(processData) {
44        console.log(`onProcessDied onProcessDied: ${JSON.stringify(processData)}`);
45    },
46    onProcessStateChanged(processData) {
47        console.log(`onProcessStateChanged onProcessStateChanged: ${JSON.stringify(processData)}`);
48    }
49};
50let observerCode = appManager.on('applicationState', applicationStateObserver);
51```
52