DEV Community

Cover image for Portrait-Landscape Switch Animation, Context Acquisition Issues, Foldable Screen Monitoring
kouwei qing
kouwei qing

Posted on

Portrait-Landscape Switch Animation, Context Acquisition Issues, Foldable Screen Monitoring

[Daily HarmonyOS Next Knowledge] Portrait-Landscape Switch Animation, Context Acquisition Issues, Foldable Screen Monitoring, Screen Width Acquisition, Free Multi-Window Mode

1. How to remove the rotation animation when entering the interface in landscape mode on a HarmonyOS foldable screen?

On a foldable screen, obtain the main window in the onWindowStageCreate method of the ability, then set setPreferredOrientation to window.Orientation.AUTO_ROTATION_RESTRICTED. When entering the interface in landscape mode, a rotation animation from portrait to landscape appears. How to remove this animation?

  • If the app needs to rotate properly during cold start:
    1. For devices supporting rotation (phones and tablets), configure auto_rotation or auto_rotation_restrict in the package information.
    2. For phones that should not rotate but tablets that should, configure follow_desktop in the package information.

For details, refer to: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/module-configuration-file-V5

2. How to acquire UIContext in HarmonyOS?

When acquiring the UIContext in the entry, the app crashes occasionally during startup. Is the usage incorrect?

The loadcontent and getMainWindow interfaces are asynchronous, while getUIContext is a synchronous interface. loadcontent and getMainWindow execute simultaneously, but their callback sequences are not guaranteed. This can lead to scenarios where getMainWindow returns a result before loadcontent completes. It is recommended to call the interface within the loadcontent callback to ensure proper timing.

Recommended example:

onWindowStageCreate(windowStage: window.WindowStage) {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index', (err, data) => {
    if (err.code) {
      hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
      return;
    }

    windowStage.getMainWindow((err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to obtain the main window.Cause:' + JSON.stringify(err))
      }
      let uiContext = data.getUIContext();
      console.log(JSON.stringify(uiContext))
    })
    hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  });
}
Enter fullscreen mode Exit fullscreen mode

3. How to monitor the unfold/fold status of a HarmonyOS foldable screen?

Refer to: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-display-V5#foldstatus10

4. Why does display.getDefaultDisplaySync() return the same screen width in HarmonyOS, even when the foldable screen is unfolded/folded?

When using display.getDefaultDisplaySync() to acquire the screen width, the value remains unchanged during foldable screen unfold/fold operations.

Solution: There is a delay in foldable screen state changes; adding a delay resolves this issue.

5. In HarmonyOS PAD free multi-window mode, does closing the app via the multitasking interface force closure or follow the app's closure process?

The free multi-window mode on PAD沿用 (adopts) the PC's pre-closure process, where app destruction requires user confirmation. This design considers file saving and data cleanup needs on PC.

Top comments (0)