Read the original article:Understanding the UIAbility Lifecycle in HarmonyOS Next
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.
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.
}
// ...
}
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
});
}
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');
}
🟦 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
}
🟦 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
}
🟦 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
}
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.
Top comments (0)