• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ApplicationStateObserver (System API)
2
3The ApplicationStateObserver module defines an observer to listen for application state changes. It can be used as an input parameter in [on](js-apis-app-ability-appManager-sys.md#appmanageronapplicationstate) 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> The APIs provided by this module are system APIs.
9
10## Modules to Import
11
12```ts
13import { appManager } from '@kit.AbilityKit';
14```
15
16## Properties
17
18**System API**: This is a system API and cannot be called by third-party applications.
19
20**System capability**: SystemCapability.Ability.AbilityRuntime.Core
21
22| Name                            | Type                   | Readable| Writable| Description  |
23| -------------------------------- | ---------------------- | ---- | ---- | ------------------ |
24| 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-sys.md).|
25| onAbilityStateChanged            | AsyncCallback\<void>   | Yes  | No | Callback invoked when the ability state changes. The parameter type passed in is [AbilityStateData](js-apis-inner-application-abilityStateData-sys.md).  |
26| onProcessCreated                 | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is created. The parameter type passed in is [ProcessData](js-apis-inner-application-processData-sys.md).         |
27| onProcessDied                     | AsyncCallback\<void>   | Yes  | No  | Callback invoked when a process is destroyed. The parameter type passed in is [ProcessData](js-apis-inner-application-processData-sys.md).         |
28| 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-sys.md).       |
29| onAppStarted<sup>12+</sup>       | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the first process of the application is created. The parameter type passed in is [AppStateData](js-apis-inner-application-appStateData-sys.md).    |
30| onAppStopped<sup>12+</sup>       | AsyncCallback\<void>   | Yes  | No  | Callback invoked when the last process of the application is destroyed. The parameter type passed in is [AppStateData](js-apis-inner-application-appStateData-sys.md).    |
31
32**Example**
33```ts
34import { appManager } from '@kit.AbilityKit';
35
36let applicationStateObserver: appManager.ApplicationStateObserver = {
37  onForegroundApplicationChanged(appStateData) {
38    console.log(`onForegroundApplicationChanged appStateData: ${JSON.stringify(appStateData)}`);
39  },
40  onAbilityStateChanged(abilityStateData) {
41    console.log(`onAbilityStateChanged onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
42  },
43  onProcessCreated(processData) {
44    console.log(`onProcessCreated onProcessCreated: ${JSON.stringify(processData)}`);
45  },
46  onProcessDied(processData) {
47    console.log(`onProcessDied onProcessDied: ${JSON.stringify(processData)}`);
48  },
49  onProcessStateChanged(processData) {
50    console.log(`onProcessStateChanged onProcessStateChanged: ${JSON.stringify(processData)}`);
51  },
52  onAppStarted(appStateData) {
53    console.log(`onAppStarted appStateData: ${JSON.stringify(appStateData)}`);
54  },
55  onAppStopped(appStateData) {
56    console.log(`onAppStopped appStateData: ${JSON.stringify(appStateData)}`);
57  }
58};
59let observerCode = appManager.on('applicationState', applicationStateObserver);
60```
61