HarmonyOS鸿蒙Next中Navigation onPop不回调问题

HarmonyOS鸿蒙Next中Navigation onPop不回调问题 我在跳转至子页面时使用了如下方式:

.onClick(() => this.pageStack.pushPathByName('ListTemplate', item, (popInfo) => {
  RDBStoreUtil.queryAllLists()
    .then(value => this.listOfList = value)
}))

并且在子页面写了:

this.pageInfo.pop({result:()=>{}})

但是并没有进行query函数的调用,接口文档说的是Object,所以我放了一个箭头函数进去。

说一下问题的核心:

我需要在子页面进行一个编辑数据库的操作,之后pop返回,但是此时页面并没有刷新,需要手动刷新一下,onAppear仅在该页初次加载之后执行,OnPageShow和OnPageHidden也尝试了,并不能实现。


更多关于HarmonyOS鸿蒙Next中Navigation onPop不回调问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

使用导航控制器NavPathStack的setInterception方法,在入参NavigationInterception中可以设置页面跳转前后的拦截回调。以跳转前拦截为例:

@Entry
@Component
struct Index {
  pathStack: NavPathStack = new NavPathStack();

  aboutToAppear(): void {
    this.pathStack.setInterception({
      willShow: (from: NavDestinationContext | 'navBar', to: NavDestinationContext | 'navBar',
        operation: NavigationOperation, animated: boolean) => {
        if (typeof from === 'string') {
          let target = to as NavDestinationContext
          console.info(`from page is navigation home -> ${target.pathInfo.name}`);
        } else if (typeof to === 'string') {
          let target = from as NavDestinationContext
          console.info(`${target.pathInfo.name} -> to page is navigation home`);
        }
      }
    })
  }

  @Builder
  pageMap(name: string) {
    PageOne()
  }

  build() {
    Navigation(this.pathStack) {
      Column({ space: 30 }) {
        Text('PageOne')
        Button('跳转')
          .onClick(() => {
            this.pathStack.pushPathByName('PageOne', null, false);
          })
      }
    }.navDestination(this.pageMap)
  }
}

@Component
struct PageOne {
  pathStack: NavPathStack = new NavPathStack();

  build() {
    NavDestination() {
      Column({ space: 30 }) {
        Text('PageOne')
        Button('跳转')
          .onClick(() => {
            this.pathStack.pop();
          })
      }
    }.title('PageOne')
    .onReady((context: NavDestinationContext) => {
      this.pathStack = context.pathStack;
    })
  }
}

更多关于HarmonyOS鸿蒙Next中Navigation onPop不回调问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


参考

Class (UIObserver)-@ohos.arkui.UIContext (UIContext)-UI界面-ArkTS API-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者

// --- 监听Nav变化 ---
  aboutToAppear()
  {
    let obs = this.getUIContext().getUIObserver();
    // 添加监听
    obs.on('navDestinationSwitch', this.callbackFunc);
  }

  aboutToDisappear()
  {
    let obs = this.getUIContext().getUIObserver();
    // 取消监听
    obs.off('navDestinationSwitch', this.callbackFunc);
  }

  private callbackFunc()
  {
    RDBStoreUtil.queryAllLists()
      .then((value) => {
        this.listOfList = value
      })
  }

使用这个也是不能实现返回之后查询的,

在HarmonyOS Next中,Navigation组件的onPop回调未触发通常与页面栈管理或生命周期配置有关。需检查页面是否通过正确的导航方式返回,例如使用router.back()而非直接关闭页面。同时确认Navigation组件在页面栈中的状态,确保未因页面销毁导致回调丢失。若使用声明式导航,需验证NavDestinationStack中目标页面的pop行为是否正常触发事件。

在HarmonyOS Next中,onPop回调未触发通常是因为在子页面调用pop()时未正确传递回调参数。根据你的代码,问题可能出现在以下几个方面:

  1. 回调参数格式pop()方法的result参数应是一个包含回调函数的对象,但你的代码中传递的是一个空箭头函数()=>{},这不会触发任何操作。正确的做法是在回调函数中执行数据刷新逻辑:
this.pageInfo.pop({
  result: () => {
    // 在这里执行返回后的数据刷新
    RDBStoreUtil.queryAllLists()
      .then(value => this.listOfList = value)
  }
})
  1. 页面生命周期:如果上述方法仍不生效,可以尝试在父页面的onPageShow生命周期中处理数据刷新,但需要配合页面栈状态管理:
onPageShow() {
  if (this.isFromPop) { // 需要自行维护状态标记
    RDBStoreUtil.queryAllLists()
      .then(value => this.listOfList = value)
    this.isFromPop = false
  }
}
  1. 替代方案:考虑使用EventHub或AppStorage进行跨页面状态管理,在子页面完成数据库操作后发布事件,父页面监听该事件并刷新数据。

建议先检查第一种方案,确保在pop()result回调中正确包含数据刷新逻辑。如果问题依旧,可能需要检查页面栈的实现是否正确,或者考虑使用状态管理方案替代回调机制。

回到顶部