1# @ohos.app.ability.ApplicationStateChangeCallback (Application Process State Change Listener) 2<!--Kit: Ability Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @SKY2001--> 5<!--Designer: @yzkp--> 6<!--Tester: @lixueqing513--> 7<!--Adviser: @huipeizi--> 8 9The module is used to listen for state changes of the current application process. For ease of description, the term "application process" will be referred to as "process" in the following sections. 10 11You can call [ApplicationContext.on('applicationStateChange')](js-apis-inner-application-applicationContext.md#applicationcontextonapplicationstatechange10) and pass in a custom ApplicationStateChangeCallback to listen for foreground/background state changes of the current process. This allows you to perform certain actions based on the process state changes, for example, tracking the duration of the process in the foreground and background, or clearing memory caches when the process moves to the background. 12 13> **NOTE** 14> 15> 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. 16> 17> The APIs of this module can be used only in the stage model. 18 19## Constraints 20 21This module allows you to listen for foreground/background state changes of the current process. If you need to listen for foreground/background state changes of the entire application, use [ApplicationStateObserver.onForegroundApplicationChanged](js-apis-inner-application-applicationStateObserver.md#applicationstateobserveronforegroundapplicationchanged). 22 23>**NOTE** 24> 25> The foreground/background state of a process is different from that of an application, as follows: 26>- Foreground/Background state of a process: If any UIAbility or UIExtensionAbility in the process is in the foreground or has a visible window, the process is considered to be in the foreground; otherwise, it is in the background. 27>- Foreground/Background state of an application: If any process under the application is in the foreground, the application is considered to be in the foreground; otherwise, it is in the background. 28 29## Modules to Import 30 31```ts 32import { ApplicationStateChangeCallback } from '@kit.AbilityKit'; 33``` 34 35## ApplicationStateChangeCallback.onApplicationForeground 36 37onApplicationForeground(): void 38 39Called when the current process switches from the background to the foreground. When this callback is triggered, it does not mean that the process is already fully in the foreground state, but rather that it is about to enter the foreground state. At this point, operations that depend on the foreground state (such as launching another UIAbility) cannot be performed. 40 41**Atomic service API**: This API can be used in atomic services since API version 11. 42 43**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 44 45**Example** 46 47For details, see [onApplicationBackground](#applicationstatechangecallbackonapplicationbackground). 48 49## ApplicationStateChangeCallback.onApplicationBackground 50 51onApplicationBackground(): void 52 53Called when the current process switches from the foreground to the background. When this callback is triggered, the process is fully in the background state, and you can perform operations suitable for the background state (for example, clearing memory caches). 54 55**Atomic service API**: This API can be used in atomic services since API version 11. 56 57**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 58 59**Example** 60 61```ts 62import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit'; 63import { BusinessError } from '@kit.BasicServicesKit'; 64 65let applicationStateChangeCallback: ApplicationStateChangeCallback = { 66 onApplicationForeground() { 67 console.info('applicationStateChangeCallback onApplicationForeground'); 68 }, 69 onApplicationBackground() { 70 console.info('applicationStateChangeCallback onApplicationBackground'); 71 } 72}; 73 74export default class MyAbility extends UIAbility { 75 onCreate() { 76 console.log('MyAbility onCreate'); 77 // 1. Obtain an applicationContext object. 78 let applicationContext = this.context.getApplicationContext(); 79 try { 80 // 2. Register a listener for the current process state changes through applicationContext. 81 if (applicationContext != undefined) { 82 applicationContext.on('applicationStateChange', applicationStateChangeCallback); 83 } 84 } catch (paramError) { 85 console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 86 } 87 console.log('Register applicationStateChangeCallback'); 88 } 89 onDestroy() { 90 let applicationContext = this.context.getApplicationContext(); 91 try { 92 // 1. Unregister the listener for the current process state changes through applicationContext. 93 if (applicationContext != undefined) { 94 applicationContext.off('applicationStateChange', applicationStateChangeCallback); 95 } 96 } catch (paramError) { 97 console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 98 } 99 } 100} 101``` 102