目录

Application Lifecycle Overview

Overview

When a user performs operations such as app startup, app foreground/background switching, and app exit, the overall application lifecycle state of the app changes accordingly. Understanding the app lifecycle helps developers perform operations such as resource application and release and service state management at the right time. This section mainly describes the application lifecycle state, the relationship between the application lifecycle and the application process and app components (UIAbility and ExtensionAbility), and how to listen for changes in the application lifecycle state.

Application Lifecycle State

[h2]Relationship Between App Lifecycle and App Process and App Components

[h2]Application Lifecycle State Changes

The foreground/background switching timing of the app process described above does not apply to 2-in-1 apps. A 2-in-1 app is switched to the foreground state by the system only when it is started, and switched to the background state by the system when it exits.

Listening to the App Lifecycle

[h2]Listening for App Startup and Exit

An app must first apply for the ohos.permission.RUNNING_STATE_OBSERVER permission. Then, the on('applicationState') method can be used to listen for the state of all apps, and the on('applicationState') method can be used to listen for the state of a specified app. By implementing the onAppStarted() method in the ApplicationStateObserver interface, you can listen for app startup. By implementing the onAppStopped() method in the ApplicationStateObserver interface, you can listen for app exit.

[h2]Listening for App Foreground/Background Changes

An app can use the on('applicationStateChange') method of ApplicationContext to listen for changes in the app's foreground/background state. When the app switches between the foreground and background, the corresponding callback is invoked, so that you can execute methods that depend on the foreground/background state or collect statistics such as the frequency of foreground/background switching.

The following uses UIAbilityContext as an example.

The callback events described above are all asynchronous callbacks and have no strict timing relationship.

import { UIAbility, ApplicationStateChangeCallback } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = '[LifecycleAbility]';
const DOMAIN = 0xF811;

export default class LifecycleAbility extends UIAbility {
  onCreate() {
    let applicationStateChangeCallback: ApplicationStateChangeCallback = {
      onApplicationForeground() {
        hilog.info(DOMAIN, TAG, 'applicationStateChangeCallback onApplicationForeground');
      },
      onApplicationBackground() {
        hilog.info(DOMAIN, TAG, 'applicationStateChangeCallback onApplicationBackground');
      }
    }

    // Obtain the applicationContext.
    let applicationContext = this.context.getApplicationContext();
    try {
      // 2. Register the application foreground/background state listener through applicationContext.
      applicationContext.on('applicationStateChange', applicationStateChangeCallback);
    } catch (paramError) {
      hilog.error(DOMAIN, TAG, `error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
    }
    hilog.info(DOMAIN, TAG, 'Register applicationStateChangeCallback');
  }
}

Application Model Features of PC/2-in-1 Devices

For an app whose deviceTypes in the module.json5 file declares support only for PC/2-in-1 devices, the triggering mechanism of its UIAbility lifecycle callbacks differs from that on other devices.

  1. When a user starts a UIAbility, the system sequentially triggers the onCreate(), onWindowStageCreate(), and onForeground() lifecycle callbacks to complete processes such as component creation and lifecycle driving.

  2. When the user switches between apps, the UIAbility remains in the foreground state, and the system does not trigger the onBackground() lifecycle callback.

  3. Only when the user actively exits the UIAbility or the app actively exits the UIAbility does the system trigger the onBackground() and onDestroy() lifecycle callbacks to complete component exit and destruction.

The process lifecycle is closely related to the UIAbility component lifecycle. Since the UIAbility remains in the foreground state for a long time in the running state, the process also remains in the foreground state for a long time, and only briefly enters the background state during normal process exit. Correspondingly, the app also remains in the foreground state for a long time, and only briefly enters the background state during normal app exit.