1# @ohos.app.ability.ApplicationStateChangeCallback (ApplicationStateChangeCallback)
2
3The **ApplicationStateChangeCallback** module provides callbacks for the application context to listen for application foreground/background state changes.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The APIs of this module can be used only in the stage model.
10
11## Modules to Import
12
13```ts
14import { ApplicationStateChangeCallback } from '@kit.AbilityKit';
15```
16
17## ApplicationStateChangeCallback.onApplicationForeground
18
19onApplicationForeground(): void
20
21Called when the application is switched from the background to the foreground.
22
23**Atomic service API**: This API can be used in atomic services since API version 11.
24
25**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
26
27**Example**
28
29For details, see [onApplicationBackground](#applicationstatechangecallbackonapplicationbackground).
30
31## ApplicationStateChangeCallback.onApplicationBackground
32
33onApplicationBackground(): void
34
35Called when the application is switched from the foreground to the background.
36
37**Atomic service API**: This API can be used in atomic services since API version 11.
38
39**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
40
41**Example**
42
43```ts
44import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
45import { BusinessError } from '@kit.BasicServicesKit';
46
47let applicationStateChangeCallback: ApplicationStateChangeCallback = {
48  onApplicationForeground() {
49    console.info('applicationStateChangeCallback onApplicationForeground');
50  },
51  onApplicationBackground() {
52    console.info('applicationStateChangeCallback onApplicationBackground');
53  }
54};
55
56export default class MyAbility extends UIAbility {
57  onCreate() {
58    console.log('MyAbility onCreate');
59    // 1. Obtain an applicationContext object.
60    let applicationContext = this.context.getApplicationContext();
61    try {
62      // 2. Use applicationContext.on() to subscribe to the 'applicationStateChange' event.
63      if (applicationContext != undefined) {
64        applicationContext.on('applicationStateChange', applicationStateChangeCallback);
65      }
66    } catch (paramError) {
67      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
68    }
69    console.log('Resgiter applicationStateChangeCallback');
70  }
71  onDestroy() {
72    let applicationContext = this.context.getApplicationContext();
73    try {
74      // 1. Use applicationContext.off() to unsubscribe from the 'applicationStateChange' event.
75      if (applicationContext != undefined) {
76        applicationContext.off('applicationStateChange', applicationStateChangeCallback);
77      }
78    } catch (paramError) {
79      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
80    }
81  }
82}
83```
84