HarmonyOS 如何拦截系统返回,实现返回时提示二次确认弹窗

HarmonyOS 如何拦截系统返回,实现返回时提示二次确认弹窗

HarmonyOS
2024-08-30 09:40:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

Demo如下

import {  MarkerOptions } from '@amap/amap_lbs_map3d'; 
import { HomeComponent, StationDetailPage } from 'home/Index'; 
import { MapViewContainer } from 'map/Index'; 
import { CustomTabBar } from '../components/CustomTabBar'; 
import { AppConstants } from '../constants/AppConstants'; 
import { PersonInfo } from 'user/Index'; 
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(AppConstants.FULL_PERCENT) 
    .height(AppConstants.FULL_PERCENT) 
    .backgroundColor($r('app.color.common_background_color')) 
  } 
} 
.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; 
  } 
}
分享
微博
QQ
微信
回复
2024-08-30 20:17:44
相关问题
实现二次侧滑退出应用
1679浏览 • 1回复 待解决
HarmonyOS cocos引擎能否二次启动
247浏览 • 1回复 待解决
class二次刷新渲染数组
621浏览 • 1回复 待解决
HarmonyOS 拦截系统返回手势问题
172浏览 • 1回复 待解决
HarmonyOS 怎么拦截返回键盘
168浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
1599浏览 • 1回复 待解决
webview拦截返回按钮。
663浏览 • 1回复 待解决