HarmonyOS 如何返回系统桌面

首页重写onBackPress方法时,如何将应用退到后台?即如何返回系统桌面?

HarmonyOS
2024-12-20 17:22:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以在页面的onBackPress中通过windowStage获取主窗口对象,然后用minimize完成主窗口的最小化。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#minimize11参考demo:

import window from '@ohos.window'
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Page240605161307072 {
  @State message: string = 'Hello World';
  onBackPress() {
    const context = getContext(this)
    window.getLastWindow(getContext()).then((data) => {
      //获取窗口对象
      let windowClass = data;
      windowClass.minimize((err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error('Failed to minimize the window. Cause: ' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in minimizing the window.');
      });
      console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    });
    return true;
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        TextInput()
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
分享
微博
QQ
微信
回复
2024-12-20 18:24:30
相关问题
HarmonyOS 返回桌面
751浏览 • 2回复 待解决
HarmonyOS 返回手机桌面
1093浏览 • 1回复 待解决
APP如何主动返回手机桌面
1172浏览 • 1回复 待解决
HarmonyOS 系统返回如何拦截
703浏览 • 1回复 待解决
如何屏蔽系统返回键?
892浏览 • 1回复 待解决
HarmonyOS 在Page中如何拦截系统返回
612浏览 • 1回复 待解决
HarmonyOS 如何禁止系统的侧滑返回
502浏览 • 1回复 待解决
如何禁用系统左右手势返回
866浏览 • 1回复 待解决
HarmonyOS 拦截系统返回手势问题
1580浏览 • 1回复 待解决
HarmonyOS 如何创建桌面快捷入口
1171浏览 • 1回复 待解决
HarmonyOS 如何生成桌面快捷方式?
955浏览 • 1回复 待解决
HarmonyOS 全模态怎么拦截系统返回
607浏览 • 1回复 待解决
如何实现悬浮窗桌面穿透
1407浏览 • 1回复 待解决