HarmonyOS鸿蒙Next中Navigation同一时刻先pop再push同一name的页面无效,需要设置NEW_INSTANCE

HarmonyOS鸿蒙Next中Navigation同一时刻先pop再push同一name的页面无效,需要设置NEW_INSTANCE 使用 Navigation 进行页面跳转,当已经在某个名为“Page01”的页面时,再在同一时刻先 pop 再 push 一个名称相同的页面,会发现页面没有发生跳转,示例如下:

Button(`pop, push Page01 (normal)`).width('80%').margin({ top: 10, bottom: 10 })
  .onClick(() => {
    this.pathStack?.pop();
    this.pathStack?.pushPath({ name: 'Page01' });
  })

这是因为连续调用多个页面栈操作方法时,中间过程会被忽略,显示最终的栈操作结果。

而上述操作栈顶页面已经为“Page01”,pop后再 push “Page01”,栈中页面无变化,所以页面不会发生跳转。

要想在这个场景 pop 已经存在的“Page01”,并且 push 一个新的“Page01”,需要设置 launchMode 为 NEW_INSTANCE,示例如下:

Button(`pop, push Page01 (new)`).width('80%').margin({ top: 10, bottom: 10 })
  .onClick(() => {
    this.pathStack?.pop();
    this.pathStack?.pushPath({ name: 'Page01' }, { launchMode: LaunchMode.NEW_INSTANCE });
  })

这样就能正常 push 一个新的“Page01”。


更多关于HarmonyOS鸿蒙Next中Navigation同一时刻先pop再push同一name的页面无效,需要设置NEW_INSTANCE的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中Navigation同一时刻先pop再push同一name的页面无效,需要设置NEW_INSTANCE的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,如果在同一时刻先poppush同一name的页面,系统默认会复用已有的页面实例,导致操作无效。为了避免这种情况,可以在push时设置NEW_INSTANCE标志,强制创建一个新的页面实例。示例代码如下:

Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_INSTANCE);
PageRouter.pushNamed("/details", intent);

这样,系统会创建一个新的页面实例,确保页面能够正常显示。

回到顶部