DEV Community

HarmonyOS
HarmonyOS

Posted on

Understanding the UIAbility Lifecycle in HarmonyOS Next

Read the original article:Understanding the UIAbility Lifecycle in HarmonyOS Next

Photo by Karsten Winegeart on Unsplash

Introduction

In HarmonyOS Next, a UIAbility is the fundamental component responsible for managing the user interface — similar in concept to an Activity in Android. As users launch or switch between applications, the associated UIAbility instances transition through a series of well-defined lifecycle states. To handle these transitions effectively, the UIAbility class provides a set of lifecycle callbacks that allow developers to track and respond to changes in the application's state.

In this article, we will examine the essential states within the UIAbility lifecycle and explain the role and best practices of each callback method to help developers better manage application behavior during lifecycle transitions.

UIAbility Lifecycle Overview

Understanding the UIAbility lifecycle is key to managing application behavior during creation, display, backgrounding, and destruction phases. HarmonyOS Next uses a state-driven model, where the system invokes specific lifecycle callbacks in response to user or system-triggered transitions. This section outlines each major lifecycle state and explains best practices for using them effectively.

UIAbility lifecycle states

Note: Critical lifecycle methods such as onCreate, onWindowStageCreate, and onForeground should only handle lightweight, essential operations. Long-running tasks should be offloaded to background threads or handled asynchronously to avoid blocking the main thread.

🟦 Create State (onCreate())
This state is triggered when the system instantiates the UIAbility class during application launch. The onCreate() callback is ideal for performing initial setup, such as defining state variables, initializing services, or setting up basic data structures.

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';


export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    // Initialize the page.
  }
  // ...
}
Enter fullscreen mode Exit fullscreen mode

Want is an object used to pass data between components. It's similar to Android’s Intent.

🟦 WindowStage Creation & Destruction (onWindowStageCreate() / onWindowStageDestroy())
Once the UIAbility is created, but before it is shown to the user, the system creates a WindowStage. The onWindowStageCreate() callback is where the UI layout is loaded and lifecycle event subscriptions occur.

You can use windowStage.loadContent() to render the initial page and subscribe to windowStageEvent to listen for events such as gaining/losing focus or transitioning between visible states.

onWindowStageCreate(windowStage) {
  this.windowStage = windowStage;

  // Load the initial UI content
  windowStage.loadContent('pages/Index', (err) => {
    if (!err.code) {
      // Content loaded successfully
    }
  });

  // Subscribe to window stage events such as focus changes and visibility
  windowStage.on('windowStageEvent', (data) => {
    // Handle window stage events here
  });
}
Enter fullscreen mode Exit fullscreen mode

Before the UI is completely torn down, the system invokes onWindowStageDestroy() and optionally onWindowStageWillDestroy() to allow resource cleanup and event subscription:

onWindowStageDestroy() {
  // Release UI resources and unsubscribe from window stage events
  this.windowStage?.off('windowStageEvent');
}
Enter fullscreen mode Exit fullscreen mode

🟦 Foreground & Background States (onForeground() / onBackground())
These methods manage app visibility. When the UI is about to become visible, onForeground() is triggered. You can resume animations, reconnect services, or re-request permissions here.

When the app moves to the background, onBackground() is called. Use it to save user data, stop sensors, or release unused resources.

onForeground() {
  // App is now visible; restart location services, sensors, or refresh UI
}

onBackground() {
  // App sent to background; save state, stop services, release memory
}
Enter fullscreen mode Exit fullscreen mode

🟦 Handling Re-Launches (onNewWant())
If a UIAbility is already active with a singleton launch mode, calling startAbility() again won’t recreate it. Instead, onNewWant() is triggered. You can use this callback to refresh or reload relevant data.

onNewWant(want, launchParam) {
  // Ability reused with new data; update UI or fetch new resources
}
Enter fullscreen mode Exit fullscreen mode

🟦 Destroy State (onDestroy())
Finally, when the ability is no longer needed, onDestroy() is invoked to release any held resources and perform cleanup.

onDestroy() {
  // Ability destroyed; perform final cleanup and save persistent data
}
Enter fullscreen mode Exit fullscreen mode

Note: Starting from API version 13, manually removing the app from recent tasks does not trigger onDestroy() — the process is killed directly.

Conclusion

The UIAbility lifecycle in HarmonyOS Next offers a clean, modular way to manage app UI states. Developers can clearly observe and understand how their apps behave across different lifecycle events.

References

https://developer.huawei.com/consumer/en/doc/harmonyos-guides/uiability-lifecycle?source=post_page-----eb8cd630fba0---------------------------------------

Written by Emine Inan

Top comments (0)