Window z-order (ZOrder) refers to the stacking order of multiple windows on the screen, which determines the front-to-back display relationship of windows.
The larger the z-order value, the closer the window is displayed to the front and can cover windows with smaller z-order values. Conversely, windows with smaller z-order values may be obscured by other windows.
For third-party apps, the z-order of a window cannot be directly modified, but the display z-order of subwindows can be indirectly affected by controlling the zLevel attribute.
The system controls the visibility and interaction priority of the user interface by managing ZOrder and zLevel.
This section systematically explains the window z-order management mechanism from five aspects: basic concepts, z-order rules, parent-child window relationships, the TopMost window mechanism, and related API examples.
ZOrder represents the window stacking order maintained internally by the system. The larger the value, the closer the window is to the front. Third-party apps cannot directly modify ZOrder.
zLevel represents the relative z-order between subwindows within an app that can be controlled by third-party apps, and is only effective for non-modal subwindows. The value range of zLevel is [-10000, 10000], and the default value is 0. The greater the zLevel value, the larger the corresponding ZOrder, and the closer the window is to the front.
Subwindows with different zLevels
Subwindows with the same zLevel
By default, the window created later has a higher z-order.
When the user taps a subwindow, its z-order is raised to the highest position within the same zLevel, implementing "tap-to-top."
If setRaiseByClickEnabled() is called with false, the raise-on-tap feature is disabled, and the z-order remains unchanged when tapped.
Z-order adjustment for windows of the same type
For windows of the same type (multiple main windows, multiple regular subwindows), the z-order can be adjusted by tapping or using shortcuts (such as Alt+Tab).
Default z-order of in-app subwindow types
The system assigns default z-orders to different types of subwindows. The priorities from high to low are as follows:
| Window Type | Creation Method |
|---|---|
| App-modal subwindow | Call the createSubWindowWithOptions() API and set isModal to true and modalityType to APPLICATION_MODALITY in the subwindow parameters SubWindowOptions. |
| Toast subwindow | Call the showToast() API and set showMode to TOP_MOST. |
| Text menu subwindow | Call the showActionMenu() API and set showInSubWindow to true. |
| Dialog subwindow | Call openCustomDialog() with dialogContent and options parameters, and set showInSubWindow to true. Call openCustomDialog() with the options parameter, and set showInSubWindow to true. Call openCustomDialogWithController(), and set showInSubWindow to true. |
| Modal subwindow | Call the createSubWindowWithOptions() API and set isModal to true and modalityType to WINDOW_MODALITY in the subwindow parameters SubWindowOptions. |
| Normal subwindow | The default subwindow created by calling the createSubWindowWithOptions() API. |
Windows of different types cannot cross their z-order ranges by tapping or through APIs.
In the freeform window state, an app's main window can call the setWindowTopmost() API with true to place itself above other app windows, achieving an always-on-top display effect that is not obscured. This should be used appropriately to avoid affecting the user experience.
Using this API requires the ohos.permission.WINDOW_TOPMOST permission.
| Feature/Operation | Control Method | Effective Scope | Applicable Scenario |
|---|---|---|---|
| Z-order | System-managed | Effective across apps | Internal rendering order management |
| zLevel | setSubWindowZLevel()/Set when creating a window | Effective only within the same app | z-order management for subwindows |
| Window topmost | setWindowTopmost() | Effective across apps | Persistent windows such as video and navigation |
| Raise by click | setRaiseByClickEnabled() | Between subwindows of the same type within the same application | Prevent accidental touches, fixed layout |