HarmonyOS Navigation作为应用视图根容器,最后一个子组件 pop无法退出页面,返回键可退出页面

应用根视图代码:

@Entry
@Component
struct EntryPage {
  pageInfos: NavPathStack = new NavPathStack()

  aboutToAppear(): void {
    this.pageInfos.replacePath({
      name:CommonConstants.SPLASH_PAGE_URL
    })
  }

  build() {
    Column() {
      //应用页面全在一个 page 中 Navigation 进行切换
      Navigation(this.pageInfos) {
      }
      .hideNavBar(true)
      .hideTitleBar(true)
    }
    .width('100%')
    .height('100%')
  }
}

SplashView 视图代码:
@Component
export  struct SplashView {

  pathStack: NavPathStack = new NavPathStack()

  build() {
    NavDestination(){
      // 模拟
      Button().onClick(()=>{
        this.pathStack.pop()
      })
    }
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack
    })
    .hideTitleBar(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.

测试结果:

Navigation 组件配置了 .hideNavBar(true)

点击按钮 pop() 页面会回退到一个空白页面

使用物理返回键 页面可正常退出

HarmonyOS
2024-12-25 09:02:21
980浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

onPop和back行为不完全一致,如果希望最后一个页面pop能退出页面可用UIAbilityContext.terminateSelf来实现

参考地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-inner-application-uiabilitycontext-V5#uiabilitycontextterminateself

参考示例:

import common from '@ohos.app.ability.common';

@Builder
export function SplashViewBuilder(param: object) {
  SplashView()
}

@Component
export struct SplashView {
  pathStack: NavPathStack = new NavPathStack()
  context = getContext(this) as common.UIAbilityContext;

  build() {
    NavDestination() {
      Column() {
        Button('pop').onClick(()=>{
          this.context.terminateSelf()
        })
      }
    }.onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack
    })
    .hideTitleBar(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.
分享
微博
QQ
微信
回复
2024-12-25 11:19:25
相关问题
HarmonyOS 如何定义一个容器
614浏览 • 1回复 待解决
销毁一个子窗口的方法
942浏览 • 1回复 待解决
HarmonyOS router.back无法退出应用
787浏览 • 1回复 待解决
如何实现一个页面显示窗口
1384浏览 • 1回复 待解决
HarmonyOS 退出应用
1293浏览 • 1回复 待解决