ApplicationStateObserver
The ApplicationStateObserver module defines an observer to listen for application state changes. It can be used as an input parameter in registerApplicationStateObserver to listen for lifecycle changes of the current application.
System capability: SystemCapability.Ability.AbilityRuntime.Core
System API: This is a system API and cannot be called by third-party applications.
Name | Type | Readable | Writable | Description | |
---|---|---|---|---|---|
onForegroundApplicationChanged8+ | AppStateData | AsyncCallback<void> | Yes | No | Callback invoked when the foreground or background state of an application changes. |
onAbilityStateChanged8+ | AbilityStateData | AsyncCallback<void> | Yes | No | Callback invoked when the ability state changes. |
onProcessCreated8+ | ProcessData | AsyncCallback<void> | Yes | No | Callback invoked when a process is created. |
onProcessDied8+ | ProcessData | AsyncCallback<void> | Yes | No | Callback invoked when a process is destroyed. |
onProcessStateChanged8+ | ProcessData | AsyncCallback<void> | Yes | No | Callback invoked when the process state is changed. |
Example
import appManager from "@ohos.application.appManager";
let applicationStateObserver = {
onForegroundApplicationChanged(appStateData) {
console.log('onForegroundApplicationChanged appStateData: ' + JSON.stringify(appStateData));
},
onAbilityStateChanged(abilityStateData) {
console.log('onAbilityStateChanged onAbilityStateChanged: ' + JSON.stringify(abilityStateData));
},
onProcessCreated(processData) {
console.log('onProcessCreated onProcessCreated: ' + JSON.stringify(processData));
},
onProcessDied(processData) {
console.log('onProcessDied onProcessDied: ' + JSON.stringify(processData));
},
onProcessStateChanged(processData) {
console.log('onProcessStateChanged onProcessStateChanged: ' + JSON.stringify(processData));
}
};
let observerCode = appManager.registerApplicationStateObserver(applicationStateObserver);