HarmonyOS 鸿蒙Next NavDestination的onshow时机问题
HarmonyOS 鸿蒙Next NavDestination的onshow时机问题
NavDestination的onShow这个在A B A场景时,A页面无法感知到B回退时的onshow事件 NavDestination生命周期
2 回复
@Entry
@Component
struct Test240820144335108 {
pageInfos: NavPathStack = new NavPathStack()
build() {
Navigation(this.pageInfos) {
Column() {
Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
// this.pageInfos.pushPath({ name: 'Test240820144335108A1' }) //将name指定的NavDestination页面信息入栈
this.pageInfos.pushPathByName('Test240820144335108A1', null) //将name指定的NavDestination页面信息入栈,传递的数据为param
})
}
}.title('NavIndex')
}
}
//Test240820144335108A1.ets
class TmpClass{
count:number=10
}
@Builder
export function Test240820144335108A1Builder(name: string, param: Object) {
Test240820144335108A1()
}
@Entry
@Component
struct Test240820144335108A1 {
pageInfos: NavPathStack = new NavPathStack()
build() {
NavDestination() {
Column() {
Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
let tmp = new TmpClass()
this.pageInfos.pushPathByName('Test240820144335108A2', tmp) //将name指定的NavDestination页面信息入栈,传递的数据为param
})
Button('popToname', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.popToName('Test240820144335108A2') //回退路由栈到第一个名为name的NavDestination页面
console.log('popToName' + JSON.stringify(this.pageInfos), '返回值' + JSON.stringify(this.pageInfos.popToName('Test240820144335108A2')))
})
Button('popToIndex', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.popToIndex(1) // 回退路由栈到index指定的NavDestination页面
console.log('popToIndex' + JSON.stringify(this.pageInfos))
})
}.width('100%').height('100%')
}.title('Test240820144335108A1')
.onShown(()=>{
console.log("页面Test240820144335108A1的OnShown方法执行了");
})
.onBackPressed(() => {
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
return true
}).onReady((context: NavDestinationContext) => {
this.pageInfos = context.pathStack
})
}
}
//Test240820144335108A2.ets
@Builder
export function Test240820144335108A2Builder(name: string, param: Object) {
Test240820144335108A2()
}
@Entry
@Component
struct Test240820144335108A2 {
pathStack: NavPathStack = new NavPathStack()
build() {
NavDestination() {
Column() {
Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pathStack.pushPathByName('Test240820144335108A1', null)
})
Button('popToname:Test240820144335108A1', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pathStack.popToName('Test240820144335108A1') //回退路由栈到第一个名为name的NavDestination页面
console.log('popToName' + JSON.stringify(this.pathStack), '返回值' + JSON.stringify(this.pathStack.popToName('Test240820144335108A1')))
})
}.width('100%').height('100%')
}.title('Test240820144335108A2')
.onBackPressed(() => {
this.pathStack.pop()
return true
})
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
console.log("current page config info is " + JSON.stringify(context.getConfigInRouteMap()))
})
.onShown(()=>{
console.log("页面Test240820144335108A2的OnShown方法执行了");
})
}
}
更多关于HarmonyOS 鸿蒙Next NavDestination的onshow时机问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
关于HarmonyOS 鸿蒙Next中NavDestination的onshow时机问题,这通常与页面生命周期管理或路由配置有关。在NavDestination路由模式下,onPageShow方法通常是在页面被显示时调用的。然而,在某些特定的路由操作下,如PUSH_WITH_RECREATE或REPLACE,前一个页面可能会被销毁或从路由栈中清除,这会导致onPageShow方法不被调用,因为页面已经不存在于路由栈中。
为了解决这个问题,你可以考虑以下几点:
- 路由模式选择:使用PUSH路由模式,而不是会销毁页面的路由模式,如PUSH_WITH_RECREATE或REPLACE。PUSH路由模式会在路由栈中保留页面,当你返回到该页面时,onPageShow方法应该会被执行。
- 页面生命周期管理:确保页面逻辑正确实现了onPageShow方法,并且没有其他错误或条件阻止该方法的执行。检查页面的生命周期管理,确保所有相关的监听器和事件处理程序都已正确设置。
- 路由配置检查:在route_map.json中正确配置页面及其生命周期监听。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。