通常返回监听方案,在各NavDestination中监听:
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkui-273-V5
希望在navigation上监听
- 可以传递方法到子页面中,每个NavDestination的onBackPressed里调用
- 监听路由信息,自己根据路由变化做判断,参考
@Watch('backPressChild')
@Provide('pageInfos')
pageInfos: NavPathStack = new NavPathStack()
- 使用customNavContentTransition监听返回
参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#customnavcontenttransition11
// Index.ets
import { SegmentButton } from '@kit.ArkUI'
import { ItemRestriction, SegmentButtonOptions, SegmentButtonTextItem } from '@ohos.arkui.advanced.SegmentButton'
import { PageOneTmp } from './navigationOne'
import { pageTwoTmp } from './navigationTwo'
import { Pages } from './navigationTwo'
@Entry
@Component
struct NavigationExample {
@Watch('backPressChildWatch') @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()
@State tabSelectedIndexes: number[] = [1]
backPressChildWatch(){
console.info("监听到路由变化:",JSON.stringify(this.pageInfos))
}
backPressChild(){
console.info("传入子页面返回回调:",JSON.stringify(this.pageInfos))
}
@State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({
buttons: [{ text: '最热' }, { text: '最新' }] as ItemRestriction<SegmentButtonTextItem>,
backgroundColor: '#f8f6f6',
selectedBackgroundColor: '#f8f6f6',
backgroundBlurStyle: BlurStyle.NONE
})
@Builder
PageMap(name: string,params:string) {
if (name === 'web') {
PageOneTmp({backF:this.backPressChild})
} else if (name === 'pageTwo') {
pageTwoTmp({ names: name, values: this.pageInfos } as Pages)
}
}
build() {
Navigation(this.pageInfos) {
Column () {
SegmentButton({
options: this.tabOptions,
selectedIndexes: $tabSelectedIndexes
}).width(80).height(25)
Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.pushPathByName('web','baidu') //将name指定的NavDestination页面信息入栈
})
}
}
.customNavContentTransition((from: NavContentInfo, to: NavContentInfo, operation: NavigationOperation) =>{
if(operation === 2)
console.info("customNavContentTransition监听到返回")
return undefined
})
.onNavBarStateChange((isVisible:Boolean)=>{
console.info("当前状态改变:isVisible",isVisible)
})
.title('NavIndex').navDestination(this.PageMap)
}
}
import { SegmentButton } from '@kit.ArkUI'
import { ItemRestriction, SegmentButtonOptions, SegmentButtonTextItem } from '@ohos.arkui.advanced.SegmentButton'
// PageOne.ets
@Component
export struct PageOneTmp {
@Consume('pageInfos') pageInfos: NavPathStack;
@State isShowModal:boolean = false
@State isShowPanel:boolean = false
@State tabSelectedIndexes: number[] = [1]
backF:() => void = () => {}
@State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({
buttons: [{ text: '最热' }, { text: '最新' }] as ItemRestriction<SegmentButtonTextItem>,
backgroundColor: '#f8f6f6',
selectedBackgroundColor: '#f8f6f6',
backgroundBlurStyle: BlurStyle.NONE
})
build() {
NavDestination() {
Column () {
Row(){
SegmentButton({
options: this.tabOptions,
selectedIndexes: $tabSelectedIndexes
}).width(80).height(25)
}
Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageInfos.pushPathByName('pageTwo',null )
})
}.width('100%').height('100%')
}.title('pageOne')
.onBackPressed(() => {
this.backF()
const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
return false
})
}
}
// PageTwo.ets
export class Pages {
names: string = ""
values: NavPathStack | null = null
}
@Builder
export function pageTwoTmp(info: Pages) {
NavDestination() {
Column () {
Button('pushPathByName', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
(info.values as NavPathStack).pushPathByName('pageOne', null)
})
}.width('100%').height('100%')
}.title('pageTwo')
.onReady((ctx: NavDestinationContext) => {
console.info("触发pageTwo的")
})
.onBackPressed(() => {
(info.values as NavPathStack).pop()
return true
})
}
- 代码里只是把三种监听方式放一起了,第1种方法传到子页面的会用到onBackPressed,不过这个是多个子页面共用1个监听回调
- 做拦截的话还是需要到onBackPressed里面做