DEV Community

Cover image for Orientation Control, Rotation Lock, Font Units, Background Cleanup Callback, Routing Selection
kouwei qing
kouwei qing

Posted on

Orientation Control, Rotation Lock, Font Units, Background Cleanup Callback, Routing Selection

[Daily HarmonyOS Next Knowledge] Orientation Control, Rotation Lock, Font Units, Background Cleanup Callback, Routing Selection

1. When HarmonyOS orientation is set to auto_rotation, the PAD screen can rotate automatically. How to set the orientation to unspecified for phone-sized screens?

Directly use: window.getLastWindow(getContext(this), (err, win) => { win.setPreferredOrientation(window.Orientation.UNSPECIFIED) })

2. How to make the "orientation sensor" and the "rotation lock" in the control center both affect screen rotation in HarmonyOS?

When listening to orientation changes via the orientation sensor, the screen rotates regardless of the rotation lock setting in the control center. Setting windowC.setPreferredOrientation(window.Orientation.AUTO_ROTATION_RESTRICTED) on the Index page is ineffective.

Expected behavior: When rotation lock is enabled, the orientation sensor should not be listened to, preventing automatic screen rotation. When rotation lock is disabled, the sensor should detect orientation changes, allowing automatic rotation.

Solution: Listen or cancel listening based on whether the user has enabled rotation lock.

Reference link: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-settings-V5#ZH-CN_TOPIC_0000001893210469__general

3. In HarmonyOS, page fonts use vp units. How to prevent them from changing with the system display size?

Convert all font sizes to vp units to fix physical pixel values.

4. When an app is cleared from the background in HarmonyOS, if stopServiceExtensionAbility is not called, will the ServiceExtensionAbility connection persist or be destroyed automatically?

In the onDestroy method of UIAbility, stopServiceExtensionAbility is called asynchronously. If the app is cleared from the background and reopened immediately, the stopServiceExtensionAbility method may not have completed, affecting business logic. Will ServiceExtensionAbility stop automatically if not manually stopped?

  • Since stopServiceExtensionAbility is asynchronous, it may still be executing after the app is cleared. Thus, reopening the app may find ServiceExtensionAbility still running.
  • ServiceExtensionAbility only stops when stopServiceExtensionAbility completes execution.

Conclusion: If stopServiceExtensionAbility is called asynchronously in onDestroy and the app is quickly reopened after being cleared, ServiceExtensionAbility may not stop immediately.

5. How to use HarmonyOS Navigation and router?

  1. If using Navigation exclusively, can we reduce the number of pages?
  2. Are there issues with an app having only one page?
  3. Does Navigation not support page jumps?
1. Current version:

(1)Cross-module page navigation:

  • For jumps between Hap and Hsp, use router for simpler routing. Declare in the page.json file and jump via URL to decouple modules.
  • For jumps between Hap and Har, use router + named routing, but this requires dynamically importing pages, increasing module coupling. Complex apps should note that Navigation has the same dynamic import capability and issues, though dynamic imports can optimize first-load speed.

(2)Intra-module page navigation:

  • Navigation, essentially component-level routing, suits intra-module jumps due to its complex component dependency management. It also offers better transition effects. Use Navigation for intra-module jumps in the current version.
2. Evolution plan:

Navigation will enhance cross-module navigation to address router limitations. Router with @Entry struggles with:

(1)Passing non-serializable parameters: Router (C++-stored data) has type restrictions; Navigation uses a unified frontend routing table with developer-defined state management.

(2)Complex animation interactions: Pages are independent, making shared element animations difficult. Navigation supports component-level routing for nested modal popups.

(3)Modal interactions: Semi-modal popups require component-level routing within the modal, not page routing.

(4)Flexible loading and destruction: @Entry generates self-executing code, limiting developer control over page lifecycle.

(5)Adaptive multi-column layout: Automatically switches to multi-column display on large windows.

Recommendation: Use Navigation for its advantages and consistent experience.

Top comments (0)