HarmonyOS 系统返回如何拦截

如何拦截系统返回,需要两次再退出应用

HarmonyOS
2024-12-24 15:09:13
874浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

如果要实现的是页面退出back事件,可以通过页面的onBackPress方法实现监听。仅有@Entry修饰的组件能获取返回事件的监听,可以通过重写onBackPress监听到返回事件的按下。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-custom-component-lifecycle-V5#onbackpress

使用NavPathStack和NavDestination的非@Entry修饰的组件可使用onBackPressed方法,具体可参考文档:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/arkui-ts/ts-basic-components-navdestination.md

import { promptAction, ShowDialogSuccessResponse } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  @Provide('appPathStack') appPathStack: NavPathStack = new NavPathStack();
  @State currentIndex: number = 0
  context = getContext(this) as common.UIAbilityContext
  aboutToAppear(): void {
  }
  @Builder
  PageMap(name: string) {
    if (name === 'homePage') {
      // HomeComponent()
    }else if (name === 'stationDetailPage'){
      // StationDetailPage()
    }else if (name === 'userPersonInfo'){
      // HomeComponent()
    }
  }
  build() {
    Navigation(this.appPathStack) {
      Stack() {
        Flex({
          direction: FlexDirection.Column,
        }) {
          Tabs({ index: this.currentIndex }) {
            TabContent() {
              // HomeComponent()
            }
            TabContent() {
              // MapViewContainer()
            }

            TabContent() {
              // ChallengeView()
            }.tabBar()

            TabContent() {
              // ActivityView({ webViewController: $webViewController })
            }

            TabContent() {
              // PersonInfo()
            }
          }
          .layoutWeight(1)
          .barHeight(0)
          .scrollable(false)
          .onChange((index) => {
            this.currentIndex = index;
          })

          // CustomTabBar({ currentIndex: $currentIndex })
        }
        .width('100%')
        .height('100%')
        .backgroundColor($r('app.media.background'))
      }
    }
    .hideTitleBar(true)
    .navDestination(this.PageMap)
    .mode(NavigationMode.Stack)

  }

  onBackPress() {
    console.info('onBackPress:拦截');
    promptAction.showDialog({
      title: "提示",
      message: "确认退出?",
      buttons: [
        {
          text: "取消",
          color: "#000000"
        },
        {
          text: "退出",
          color: "#555555"
        }
      ]
    }).then((data: ShowDialogSuccessResponse) => {
      console.info('showDialog success, click button: ' + data.index);
      if (data.index == 1) {
        this.context.terminateSelf()
      }
    })
    return true;
  }
}
  • 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.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
分享
微博
QQ
微信
回复
2024-12-24 18:10:49


相关问题
HarmonyOS 在Page中如何拦截系统返回
587浏览 • 1回复 待解决
HarmonyOS 拦截系统返回手势问题
1536浏览 • 1回复 待解决
HarmonyOS 全模态怎么拦截系统返回
572浏览 • 1回复 待解决
HarmonyOS 如何拦截物理返回
600浏览 • 1回复 待解决
HarmonyOS 如何拦截UIAbility的返回
488浏览 • 1回复 待解决
HarmonyOS 弹窗中如何拦截返回
676浏览 • 1回复 待解决
HarmonyOS RN如何拦截返回事件
494浏览 • 1回复 待解决
HarmonyOS 怎么拦截返回键盘
1048浏览 • 1回复 待解决
HarmonyOS 返回及侧滑返回无法拦截事件
1270浏览 • 1回复 待解决
HarmonyOS UIAbility 侧滑时如何拦截返回
636浏览 • 1回复 待解决
webview拦截返回按钮。
1575浏览 • 1回复 待解决
HarmonyOS Web组件拦截返回按钮
773浏览 • 1回复 待解决
有谁知道如何拦截住页面返回
1135浏览 • 3回复 待解决
HarmonyOS web组件拦截返回手势
610浏览 • 1回复 待解决
HarmonyOS 如何返回系统桌面
867浏览 • 1回复 待解决
HarmonyOS 侧滑返回事件拦截与绑定
2319浏览 • 1回复 待解决