已合并
add navigation sample #82159
add navigation sample #82159
已合并
共 7 个文件变更+1712-0
| @@ -0,0 +1,49 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import router from '@ohos.router'; | ||
| 16 | + | ||
| 17 | +interface ListCategories { | ||
| 18 | + title:string, | ||
| 19 | + url:string | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +@Entry | ||
| 23 | +@Component | ||
| 24 | +struct Index { | ||
| 25 | + @State message: string = '【基础能力】新增Navigation页面生命周期回调' | ||
| 26 | + private items : ListCategories[] = [ | ||
| 27 | + {title:'1 未指定id监听',url:'pages/NavigationLifeCycle/MainWindow'}, | ||
| 28 | + {title:'2 指定id监听',url:'pages/NavigationLifeCycle/MainWindowID'}, | ||
| 29 | + {title:'3 willshow和didshow执行过程中,跳出弹窗',url:'pages/NavigationLifeCycle/Test1'}, | ||
| 30 | + {title:'4 自定义动画',url:'pages/NavigationLifeCycle/customNavContentTransition'}, | ||
| 31 | + ] | ||
| 32 | + | ||
| 33 | + build() { | ||
| 34 | + Column() { | ||
| 35 | + Text(this.message).fontSize(22).fontColor(0xFFFFFF).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(5).backgroundColor("#1770b9") | ||
| 36 | + List({space:'10vp'}) { | ||
| 37 | + ForEach(this.items,(item : ListCategories) => { | ||
| 38 | + ListItem() { | ||
| 39 | + Button(item.title) | ||
| 40 | + .fontSize(15) | ||
| 41 | + .onClick(() => { | ||
| 42 | + router.pushUrl({url:item.url}) | ||
| 43 | + }) | ||
| 44 | + } | ||
| 45 | + }) | ||
| 46 | + }.alignListItem(ListItemAlign.Center).margin({top:'10vp'}).width('100%').height('90%') | ||
| 47 | + }.width('100%').height('100%').backgroundColor(0xE0FFFF) | ||
| 48 | + } | ||
| 49 | +} | ||
| @@ -0,0 +1,486 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import router from '@ohos.router'; | ||
| 16 | +import observer from '@ohos.arkui.observer'; | ||
| 17 | +import window from '@ohos.window'; | ||
| 18 | +import base from '@ohos.base'; | ||
| 19 | +import { hilog_log, SR_NO_MAP, printPathStack } from '../utils/Index'; | ||
| 20 | + | ||
| 21 | +@Entry | ||
| 22 | +@Component | ||
| 23 | +struct NavigationApi { | ||
| 24 | + @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack(); | ||
| 25 | + private myWindow: window.Window | null = null; | ||
| 26 | + | ||
| 27 | + navDesUpdateCallback(info: NavDestinationInfo) { | ||
| 28 | + console.log(printPathStack(info, SR_NO_MAP.NavigationLifeCycle)) | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + observerOn(){ | ||
| 32 | + observer.on('navDestinationUpdate', this.navDesUpdateCallback); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + observerOff(){ | ||
| 36 | + observer.off('navDestinationUpdate', this.navDesUpdateCallback); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + onPageShow() { | ||
| 40 | + console.info('testTag MainWindow-pageA onPageShow'); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + onPageHide() { | ||
| 44 | + console.info('testTag MainWindow-pageA onPageHide'); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + onBackPress() { | ||
| 48 | + console.info('testTag MainWindow-pageA onBackPress'); | ||
| 49 | + return false | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + aboutToAppear() { | ||
| 53 | + console.info('testTag MainWindow-pageA aboutToAppear'); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + aboutToDisappear() { | ||
| 57 | + observer.off('navDestinationUpdate', this.navDesUpdateCallback); | ||
| 58 | + console.info('testTag MainWindow-pageA aboutToDisappear'); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Builder | ||
| 62 | + pageOneTmp() { | ||
| 63 | + NavDestination() { | ||
| 64 | + Column({ space: 10 }) { | ||
| 65 | + } | ||
| 66 | + }.title('pageOne').backgroundColor(Color.Pink) | ||
| 67 | + .onBackPressed(() => { | ||
| 68 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageOne onBackPress`) | ||
| 69 | + this.pageInfos.pop() | ||
| 70 | + return true | ||
| 71 | + }) | ||
| 72 | + .onWillAppear(()=>{ | ||
| 73 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillAppear") | ||
| 74 | + }) | ||
| 75 | + .onAppear(()=>{ | ||
| 76 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onAppear") | ||
| 77 | + }) | ||
| 78 | + .onWillShow(()=>{ | ||
| 79 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillShow") | ||
| 80 | + }) | ||
| 81 | + .onShown(()=>{ | ||
| 82 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onShown") | ||
| 83 | + }) | ||
| 84 | + .onWillHide(()=>{ | ||
| 85 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillHide") | ||
| 86 | + }) | ||
| 87 | + .onHidden(()=>{ | ||
| 88 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onHidden") | ||
| 89 | + }) | ||
| 90 | + .onWillDisappear(()=>{ | ||
| 91 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillDisappear") | ||
| 92 | + }) | ||
| 93 | + .onDisAppear(()=>{ | ||
| 94 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onDisAppear") | ||
| 95 | + }) | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + @Builder | ||
| 99 | + pageTwoTmp() { | ||
| 100 | + NavDestination() { | ||
| 101 | + Column({ space: 10 }) { | ||
| 102 | + } | ||
| 103 | + }.title('pageTwo').backgroundColor(Color.Orange) | ||
| 104 | + .onBackPressed(() => { | ||
| 105 | + this.pageInfos.pop() | ||
| 106 | + return true | ||
| 107 | + }) | ||
| 108 | + .onWillAppear(()=>{ | ||
| 109 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillAppear") | ||
| 110 | + }) | ||
| 111 | + .onAppear(()=>{ | ||
| 112 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onAppear") | ||
| 113 | + }) | ||
| 114 | + .onWillShow(()=>{ | ||
| 115 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillShow") | ||
| 116 | + }) | ||
| 117 | + .onShown(()=>{ | ||
| 118 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onShown") | ||
| 119 | + }) | ||
| 120 | + .onWillHide(()=>{ | ||
| 121 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillHide") | ||
| 122 | + }) | ||
| 123 | + .onHidden(()=>{ | ||
| 124 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onHidden") | ||
| 125 | + }) | ||
| 126 | + .onWillDisappear(()=>{ | ||
| 127 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillDisappear") | ||
| 128 | + }) | ||
| 129 | + .onDisAppear(()=>{ | ||
| 130 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onDisAppear") | ||
| 131 | + }) | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + @Builder | ||
| 135 | + pageThreeTmp() { | ||
| 136 | + NavDestination() { | ||
| 137 | + Column({ space: 10 }) { | ||
| 138 | + } | ||
| 139 | + }.title('pageThree') | ||
| 140 | + .mode(NavDestinationMode.DIALOG) | ||
| 141 | + .onBackPressed(() => { | ||
| 142 | + this.pageInfos.pop() | ||
| 143 | + return true | ||
| 144 | + }) | ||
| 145 | + .onWillAppear(()=>{ | ||
| 146 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillAppear") | ||
| 147 | + }) | ||
| 148 | + .onAppear(()=>{ | ||
| 149 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onAppear") | ||
| 150 | + }) | ||
| 151 | + .onWillShow(()=>{ | ||
| 152 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillShow") | ||
| 153 | + }) | ||
| 154 | + .onShown(()=>{ | ||
| 155 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onShown") | ||
| 156 | + }) | ||
| 157 | + .onWillHide(()=>{ | ||
| 158 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillHide") | ||
| 159 | + }) | ||
| 160 | + .onHidden(()=>{ | ||
| 161 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onHidden") | ||
| 162 | + }) | ||
| 163 | + .onWillDisappear(()=>{ | ||
| 164 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillDisappear") | ||
| 165 | + }) | ||
| 166 | + .onDisAppear(()=>{ | ||
| 167 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onDisAppear") | ||
| 168 | + }) | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + @Builder | ||
| 172 | + pageFourTmp() { | ||
| 173 | + NavDestination() { | ||
| 174 | + Column({ space: 10 }) { | ||
| 175 | + } | ||
| 176 | + }.title('pageFour') | ||
| 177 | + .mode(NavDestinationMode.DIALOG) | ||
| 178 | + .onBackPressed(() => { | ||
| 179 | + this.pageInfos.pop() | ||
| 180 | + return true | ||
| 181 | + }) | ||
| 182 | + .onWillAppear(()=>{ | ||
| 183 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillAppear") | ||
| 184 | + }) | ||
| 185 | + .onAppear(()=>{ | ||
| 186 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onAppear") | ||
| 187 | + }) | ||
| 188 | + .onWillShow(()=>{ | ||
| 189 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillShow") | ||
| 190 | + }) | ||
| 191 | + .onShown(()=>{ | ||
| 192 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onShown") | ||
| 193 | + }) | ||
| 194 | + .onWillHide(()=>{ | ||
| 195 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillHide") | ||
| 196 | + }) | ||
| 197 | + .onHidden(()=>{ | ||
| 198 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onHidden") | ||
| 199 | + }) | ||
| 200 | + .onWillDisappear(()=>{ | ||
| 201 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillDisappear") | ||
| 202 | + }) | ||
| 203 | + .onDisAppear(()=>{ | ||
| 204 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onDisAppear") | ||
| 205 | + }) | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + @Builder | ||
| 209 | + PageMap(name: string) { | ||
| 210 | + if (name === 'pageOne') { | ||
| 211 | + this.pageOneTmp() | ||
| 212 | + } else if (name === 'pageTwo') { | ||
| 213 | + this.pageTwoTmp() | ||
| 214 | + } else if (name === 'pageThree') { | ||
| 215 | + this.pageThreeTmp() | ||
| 216 | + } else if (name === 'pageFour') { | ||
| 217 | + this.pageFourTmp() | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + | ||
| 222 | + createWindow() { | ||
| 223 | + try { | ||
| 224 | + window.createWindow({name: "testWindow", windowType: window.WindowType.TYPE_DIALOG, ctx: getContext(this)}) | ||
| 225 | + .then((windowObj: window.Window) => { | ||
| 226 | + console.log(`testTag success to createWindow`) | ||
| 227 | + this.myWindow = windowObj; | ||
| 228 | + this.myWindow.showWindow().then(() => { | ||
| 229 | + console.log(`testTag success to showWindow`) | ||
| 230 | + this.myWindow?.resize(1000, 2200); | ||
| 231 | + this.myWindow?.moveWindowTo(50, 200); | ||
| 232 | + let ls = new LocalStorage(); | ||
| 233 | + this.myWindow?.loadContent('pages/NavigationLifeCycle/SubWindow', ls) | ||
| 234 | + .then(() => { | ||
| 235 | + console.log(`testTag success to loadContent`) | ||
| 236 | + }).catch((e: base.BusinessError) => { | ||
| 237 | + console.log(`testTag failed to loadContent: ${JSON.stringify(e)}`) | ||
| 238 | + }) | ||
| 239 | + }).catch((e: base.BusinessError) => { | ||
| 240 | + console.log(`testTag failed to showWindow: ${JSON.stringify(e)}`) | ||
| 241 | + }); | ||
| 242 | + }).catch((e: base.BusinessError) => { | ||
| 243 | + console.log(`testTag failed to createWindow: ${JSON.stringify(e)}`) | ||
| 244 | + }) | ||
| 245 | + } catch (e) { | ||
| 246 | + console.log(`testTag catch exception: ${JSON.stringify(e)}`) | ||
| 247 | + } | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + | ||
| 251 | + @State getAllPathName:string = '' | ||
| 252 | + @State disableAnimation: boolean = false | ||
| 253 | + @Styles buttonStyles() { | ||
| 254 | + .height(30) | ||
| 255 | + .margin(3) | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + build() { | ||
| 259 | + Column({space:5}){ | ||
| 260 | + Text('pageA页面').fontSize(18).fontWeight(FontWeight.Bold) | ||
| 261 | + | ||
| 262 | + Button('全局是否禁用转场动画,disableAnimation: ' + this.disableAnimation) | ||
| 263 | + .key('disableAnimation') | ||
| 264 | + .onClick(()=>{ | ||
| 265 | + this.disableAnimation = !this.disableAnimation | ||
| 266 | + this.pageInfos.disableAnimation(this.disableAnimation) | ||
| 267 | + }) | ||
| 268 | + | ||
| 269 | + Row({space:5}){ | ||
| 270 | + Button('observerOn') | ||
| 271 | + .fontSize(12) | ||
| 272 | + .buttonStyles() | ||
| 273 | + .onClick(() => { | ||
| 274 | + this.observerOn() | ||
| 275 | + }) | ||
| 276 | + | ||
| 277 | + Button('observerOff') | ||
| 278 | + .fontSize(12) | ||
| 279 | + .buttonStyles() | ||
| 280 | + .onClick(() => { | ||
| 281 | + this.observerOff() | ||
| 282 | + }) | ||
| 283 | + } | ||
| 284 | + Row() { | ||
| 285 | + Button('开启子窗口') | ||
| 286 | + .fontSize(12) | ||
| 287 | + .buttonStyles() | ||
| 288 | + .onClick(() => { | ||
| 289 | + this.createWindow(); | ||
| 290 | + }) | ||
| 291 | + | ||
| 292 | + Button('跳转到pageB页面') | ||
| 293 | + .fontSize(12) | ||
| 294 | + .buttonStyles() | ||
| 295 | + .onClick(()=>{ | ||
| 296 | + router.pushUrl({url:'pages/NavigationLifeCycle/PageB'}) | ||
| 297 | + }) | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + Flex({ wrap: FlexWrap.Wrap, justifyContent:FlexAlign.Center }) { | ||
| 301 | + | ||
| 302 | + Button('push pageOne') | ||
| 303 | + .onClick(() => { | ||
| 304 | + this.pageInfos.pushPath({ name: 'pageOne' }) | ||
| 305 | + }) | ||
| 306 | + .fontSize(12) | ||
| 307 | + .buttonStyles() | ||
| 308 | + | ||
| 309 | + Button('push pageTwo') | ||
| 310 | + .onClick(() => { | ||
| 311 | + this.pageInfos.pushPath({ name: 'pageTwo' }) | ||
| 312 | + }) | ||
| 313 | + .fontSize(12) | ||
| 314 | + .buttonStyles() | ||
| 315 | + | ||
| 316 | + Button('pop') | ||
| 317 | + .onClick(() => { | ||
| 318 | + this.pageInfos.pop() | ||
| 319 | + }) | ||
| 320 | + .fontSize(12) | ||
| 321 | + .buttonStyles() | ||
| 322 | + | ||
| 323 | + Button('push pageThree') | ||
| 324 | + .onClick(() => { | ||
| 325 | + this.pageInfos.pushPath({ name: 'pageThree' }) | ||
| 326 | + }) | ||
| 327 | + .fontSize(12) | ||
| 328 | + .buttonStyles() | ||
| 329 | + | ||
| 330 | + Button('push pageFour') | ||
| 331 | + .onClick(() => { | ||
| 332 | + this.pageInfos.pushPath({ name: 'pageFour' }) | ||
| 333 | + }) | ||
| 334 | + .fontSize(12) | ||
| 335 | + .buttonStyles() | ||
| 336 | + | ||
| 337 | + Button('pop*3') | ||
| 338 | + .onClick(() => { | ||
| 339 | + this.pageInfos.pop() | ||
| 340 | + this.pageInfos.pop() | ||
| 341 | + this.pageInfos.pop() | ||
| 342 | + }) | ||
| 343 | + .fontSize(12) | ||
| 344 | + .buttonStyles() | ||
| 345 | + | ||
| 346 | + Button('replacePath pageTwo') | ||
| 347 | + .onClick(() => { | ||
| 348 | + this.pageInfos.replacePath({name: 'pageTwo', param: null}) | ||
| 349 | + }) | ||
| 350 | + .fontSize(12) | ||
| 351 | + .buttonStyles() | ||
| 352 | + | ||
| 353 | + Button('replacePath pageThree') | ||
| 354 | + .onClick(() => { | ||
| 355 | + this.pageInfos.replacePath({name: 'pageThree', param: null}) | ||
| 356 | + }) | ||
| 357 | + .fontSize(12) | ||
| 358 | + .buttonStyles() | ||
| 359 | + | ||
| 360 | + Button('clear') | ||
| 361 | + .onClick(() => { | ||
| 362 | + this.pageInfos.clear() | ||
| 363 | + }) | ||
| 364 | + .fontSize(12) | ||
| 365 | + .buttonStyles() | ||
| 366 | + | ||
| 367 | + Button('replacePath pageFour') | ||
| 368 | + .onClick(() => { | ||
| 369 | + this.pageInfos.replacePath({name: 'pageFour', param: null}) | ||
| 370 | + }) | ||
| 371 | + .fontSize(12) | ||
| 372 | + .buttonStyles() | ||
| 373 | + | ||
| 374 | + Button('removeByIndexes [0]') | ||
| 375 | + .onClick(() => { | ||
| 376 | + this.pageInfos.removeByIndexes([0]) | ||
| 377 | + }) | ||
| 378 | + .fontSize(12) | ||
| 379 | + .buttonStyles() | ||
| 380 | + | ||
| 381 | + Button('removeByIndexes [1]') | ||
| 382 | + .onClick(() => { | ||
| 383 | + this.pageInfos.removeByIndexes([1]) | ||
| 384 | + }) | ||
| 385 | + .fontSize(12) | ||
| 386 | + .buttonStyles() | ||
| 387 | + | ||
| 388 | + Button('removeByIndexes [2]') | ||
| 389 | + .onClick(() => { | ||
| 390 | + this.pageInfos.removeByIndexes([2]) | ||
| 391 | + }) | ||
| 392 | + .fontSize(12) | ||
| 393 | + .buttonStyles() | ||
| 394 | + | ||
| 395 | + Button('removeByIndexes [2,4]') | ||
| 396 | + .onClick(() => { | ||
| 397 | + this.pageInfos.removeByIndexes([2,4]) | ||
| 398 | + }) | ||
| 399 | + .fontSize(12) | ||
| 400 | + .buttonStyles() | ||
| 401 | + | ||
| 402 | + Button('moveIndexToTop 0') | ||
| 403 | + .onClick(() => { | ||
| 404 | + this.pageInfos.moveIndexToTop(0) | ||
| 405 | + }) | ||
| 406 | + .fontSize(12) | ||
| 407 | + .buttonStyles() | ||
| 408 | + | ||
| 409 | + Button('moveIndexToTop 1') | ||
| 410 | + .onClick(() => { | ||
| 411 | + this.pageInfos.moveIndexToTop(1) | ||
| 412 | + }) | ||
| 413 | + .fontSize(12) | ||
| 414 | + .buttonStyles() | ||
| 415 | + | ||
| 416 | + Button('moveIndexToTop 2') | ||
| 417 | + .onClick(() => { | ||
| 418 | + this.pageInfos.moveIndexToTop(2) | ||
| 419 | + }) | ||
| 420 | + .fontSize(12) | ||
| 421 | + .buttonStyles() | ||
| 422 | + | ||
| 423 | + Button('popToIndex 0') | ||
| 424 | + .onClick(() => { | ||
| 425 | + this.pageInfos.popToIndex(0) | ||
| 426 | + }) | ||
| 427 | + .fontSize(12) | ||
| 428 | + .buttonStyles() | ||
| 429 | + | ||
| 430 | + Button('popToName pageThree') | ||
| 431 | + .onClick(() => { | ||
| 432 | + this.pageInfos.popToName('pageThree') | ||
| 433 | + }) | ||
| 434 | + .fontSize(12) | ||
| 435 | + .buttonStyles() | ||
| 436 | + | ||
| 437 | + Button('popToName pageOne') | ||
| 438 | + .onClick(() => { | ||
| 439 | + this.pageInfos.popToName('pageOne') | ||
| 440 | + }) | ||
| 441 | + .fontSize(12) | ||
| 442 | + .buttonStyles() | ||
| 443 | + | ||
| 444 | + Button("push*3,pop*2") | ||
| 445 | + .fontSize(12) | ||
| 446 | + .buttonStyles() | ||
| 447 | + .onClick(()=>{ | ||
| 448 | + this.pageInfos.pushPathByName('pageOne', null) | ||
| 449 | + this.pageInfos.pushPathByName('pageTwo', null) | ||
| 450 | + this.pageInfos.pushPathByName('pageThree', null) | ||
| 451 | + this.pageInfos.pop() | ||
| 452 | + this.pageInfos.pop() | ||
| 453 | + }) | ||
| 454 | + | ||
| 455 | + Button("move*3,pop*2") | ||
| 456 | + .fontSize(12) | ||
| 457 | + .buttonStyles() | ||
| 458 | + .onClick(()=>{ | ||
| 459 | + this.pageInfos.moveToTop('pageOne') | ||
| 460 | + this.pageInfos.moveToTop('pageTwo') | ||
| 461 | + this.pageInfos.moveToTop('pageThree') | ||
| 462 | + this.pageInfos.pop() | ||
| 463 | + this.pageInfos.pop() | ||
| 464 | + }) | ||
| 465 | + | ||
| 466 | + Button('push 不存在的navDestination') | ||
| 467 | + .onClick(() => { | ||
| 468 | + this.pageInfos.pushPathByName('pageFive', null) | ||
| 469 | + }) | ||
| 470 | + .fontSize(12) | ||
| 471 | + .buttonStyles() | ||
| 472 | + | ||
| 473 | + }.width('100%').height('48%') | ||
| 474 | + | ||
| 475 | + Navigation(this.pageInfos) { | ||
| 476 | + Column({space:10}) { | ||
| 477 | + } | ||
| 478 | + } | ||
| 479 | + .borderWidth(1) | ||
| 480 | + .height('30%') | ||
| 481 | + .title('Navigation') | ||
| 482 | + .navDestination(this.PageMap) | ||
| 483 | + } | ||
| 484 | + | ||
| 485 | + } | ||
| 486 | +} | ||
| @@ -0,0 +1,197 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import observer from '@ohos.arkui.observer'; | ||
| 16 | +import { hilog_log, printPathStack, SR_NO_MAP } from '../utils/Index'; | ||
| 17 | + | ||
| 18 | +@Entry | ||
| 19 | +@Component | ||
| 20 | +struct NavigationApiID { | ||
| 21 | + @State pageInfos: NavPathStack = new NavPathStack(); | ||
| 22 | + @State pageInfos2: NavPathStack = new NavPathStack(); | ||
| 23 | + | ||
| 24 | + navDesUpdateCallback(info: NavDestinationInfo) { | ||
| 25 | + console.log(printPathStack(info, SR_NO_MAP.NavigationLifeCycle)) | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + observerOn(){ | ||
| 29 | + observer.on('navDestinationUpdate', {navigationId:'nav1'}, this.navDesUpdateCallback); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + observerOff(){ | ||
| 33 | + observer.off('navDestinationUpdate',{navigationId:'nav1'}, this.navDesUpdateCallback); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + onPageShow() { | ||
| 37 | + console.info('testTag pageAID onPageShow'); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + onPageHide() { | ||
| 41 | + console.info('testTag pageAID onPageHide'); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + onBackPress() { | ||
| 45 | + console.info('testTag pageAID onBackPress'); | ||
| 46 | + return false | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + aboutToAppear() { | ||
| 50 | + console.info('testTag pageAID aboutToAppear'); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + aboutToDisappear() { | ||
| 54 | + observer.off('navDestinationUpdate',{navigationId:'nav1'}, this.navDesUpdateCallback); | ||
| 55 | + console.info('testTag pageAID aboutToDisappear'); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Builder | ||
| 59 | + pageOneTmp() { | ||
| 60 | + NavDestination() { | ||
| 61 | + Column({ space: 10 }) { | ||
| 62 | + } | ||
| 63 | + }.title('pageOne').backgroundColor(Color.Pink) | ||
| 64 | + .onBackPressed(() => { | ||
| 65 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageOne onBackPress`) | ||
| 66 | + return false | ||
| 67 | + }) | ||
| 68 | + .onWillAppear(()=>{ | ||
| 69 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillAppear") | ||
| 70 | + }) | ||
| 71 | + .onAppear(()=>{ | ||
| 72 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onAppear") | ||
| 73 | + }) | ||
| 74 | + .onWillShow(()=>{ | ||
| 75 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillShow") | ||
| 76 | + }) | ||
| 77 | + .onShown(()=>{ | ||
| 78 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onShown") | ||
| 79 | + }) | ||
| 80 | + .onWillHide(()=>{ | ||
| 81 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillHide") | ||
| 82 | + }) | ||
| 83 | + .onHidden(()=>{ | ||
| 84 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onHidden") | ||
| 85 | + }) | ||
| 86 | + .onWillDisappear(()=>{ | ||
| 87 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillDisappear") | ||
| 88 | + }) | ||
| 89 | + .onDisAppear(()=>{ | ||
| 90 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onDisAppear") | ||
| 91 | + }) | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + @Builder | ||
| 95 | + pageTwoTmp() { | ||
| 96 | + NavDestination() { | ||
| 97 | + Column({ space: 10 }) { | ||
| 98 | + } | ||
| 99 | + }.title('pageTwo').backgroundColor(Color.Orange) | ||
| 100 | + .onBackPressed(() => { | ||
| 101 | + console.log(`testTag pageTwo onBackPress`) | ||
| 102 | + return false | ||
| 103 | + }) | ||
| 104 | + .onWillAppear(()=>{ | ||
| 105 | + console.log("testTag pageTwo onWillAppear") | ||
| 106 | + }) | ||
| 107 | + .onAppear(()=>{ | ||
| 108 | + console.log("testTag pageTwo onAppear") | ||
| 109 | + }) | ||
| 110 | + .onWillShow(()=>{ | ||
| 111 | + console.log("testTag pageTwo onWillShow") | ||
| 112 | + }) | ||
| 113 | + .onShown(()=>{ | ||
| 114 | + console.log("testTag pageTwo onShown") | ||
| 115 | + }) | ||
| 116 | + .onWillHide(()=>{ | ||
| 117 | + console.log("testTag pageTwo onWillHide") | ||
| 118 | + }) | ||
| 119 | + .onHidden(()=>{ | ||
| 120 | + console.log("testTag pageTwo onHidden") | ||
| 121 | + }) | ||
| 122 | + .onWillDisappear(()=>{ | ||
| 123 | + console.log("testTag pageTwo onWillDisappear") | ||
| 124 | + }) | ||
| 125 | + .onDisAppear(()=>{ | ||
| 126 | + console.log("testTag pageTwo onDisAppear") | ||
| 127 | + }) | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + | ||
| 131 | + @Builder | ||
| 132 | + PageMap(name: string) { | ||
| 133 | + if (name === 'pageOne') { | ||
| 134 | + this.pageOneTmp() | ||
| 135 | + } else if (name === 'pageTwo') { | ||
| 136 | + this.pageTwoTmp() | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + @Builder | ||
| 141 | + PageMap2(name: string) { | ||
| 142 | + if (name === 'pageOne') { | ||
| 143 | + this.pageOneTmp() | ||
| 144 | + } else if (name === 'pageTwo') { | ||
| 145 | + this.pageTwoTmp() | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + | ||
| 150 | + build() { | ||
| 151 | + Column({space:5}){ | ||
| 152 | + Text('指定id监听').fontSize(18).fontWeight(FontWeight.Bold) | ||
| 153 | + | ||
| 154 | + Row({space:5}){ | ||
| 155 | + Button('observerOn') | ||
| 156 | + .onClick(() => { | ||
| 157 | + this.observerOn() | ||
| 158 | + }) | ||
| 159 | + | ||
| 160 | + Button('observerOff') | ||
| 161 | + .onClick(() => { | ||
| 162 | + this.observerOff() | ||
| 163 | + }) | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + Navigation(this.pageInfos) { | ||
| 167 | + Column({space:10}) { | ||
| 168 | + Button('push pageOne') | ||
| 169 | + .key('firstPushPageOne') | ||
| 170 | + .onClick(() => { | ||
| 171 | + this.pageInfos.pushPath({ name: 'pageOne' }) | ||
| 172 | + }).margin(5) | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + .borderWidth(1) | ||
| 176 | + .height('30%') | ||
| 177 | + .title('Navigation1') | ||
| 178 | + .id('nav1') | ||
| 179 | + .navDestination(this.PageMap) | ||
| 180 | + | ||
| 181 | + Navigation(this.pageInfos2) { | ||
| 182 | + Column({space:10}) { | ||
| 183 | + Button('push pageOne') | ||
| 184 | + .key('secondPushPageOne') | ||
| 185 | + .onClick(() => { | ||
| 186 | + this.pageInfos2.pushPath({ name: 'pageOne' }) | ||
| 187 | + }).margin(5) | ||
| 188 | + } | ||
| 189 | + } | ||
| 190 | + .borderWidth(1) | ||
| 191 | + .height('30%') | ||
| 192 | + .title('Navigation2') | ||
| 193 | + .navDestination(this.PageMap2) | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + } | ||
| 197 | +} | ||
| @@ -0,0 +1,50 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import router from '@ohos.router'; | ||
| 16 | +@Entry | ||
| 17 | +@Component | ||
| 18 | +struct PageB { | ||
| 19 | + | ||
| 20 | + onPageShow() { | ||
| 21 | + console.info('testTag MainWindow-pageB onPageShow'); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + onPageHide() { | ||
| 25 | + console.info('testTag MainWindow-pageB onPageHide'); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + onBackPress() { | ||
| 29 | + console.info('testTag MainWindow-pageB onBackPress'); | ||
| 30 | + return false | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + aboutToAppear() { | ||
| 34 | + console.info('testTag MainWindow-pageB aboutToAppear'); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + aboutToDisappear() { | ||
| 38 | + console.info('testTag MainWindow-pageB aboutToDisappear'); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + build() { | ||
| 42 | + Column({space:10}){ | ||
| 43 | + Text('pageB页面').fontSize(18).fontWeight(FontWeight.Bold) | ||
| 44 | + Button('router back') | ||
| 45 | + .onClick(()=>{ | ||
| 46 | + router.back() | ||
| 47 | + }) | ||
| 48 | + }.width('100%') | ||
| 49 | + } | ||
| 50 | +} | ||
| @@ -0,0 +1,379 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import window from '@ohos.window'; | ||
| 16 | +import { BusinessError } from '@ohos.base'; | ||
| 17 | +import { hilog_log, SR_NO_MAP } from '../utils/Index'; | ||
| 18 | + | ||
| 19 | +@Entry | ||
| 20 | +@Component | ||
| 21 | +struct NavigationApiSub { | ||
| 22 | + @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack(); | ||
| 23 | + private curWindow: window.Window | null = null; | ||
| 24 | + | ||
| 25 | + onPageShow() { | ||
| 26 | + console.info('testTag SubWindow-PageA onPageShow'); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + onPageHide() { | ||
| 30 | + console.info('testTag SubWindow-PageA onPageHide'); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + onBackPress() { | ||
| 34 | + console.info('testTag SubWindow-PageA onBackPress'); | ||
| 35 | + return false | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + aboutToAppear() { | ||
| 39 | + console.info('testTag SubWindow-PageA aboutToAppear'); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + aboutToDisappear() { | ||
| 43 | + console.info('testTag SubWindow-PageA aboutToDisappear'); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @Styles buttonStyles() { | ||
| 47 | + .height(30) | ||
| 48 | + .margin(3) | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @Builder | ||
| 52 | + pageOneTmp() { | ||
| 53 | + NavDestination() { | ||
| 54 | + Column({ space: 10 }) { | ||
| 55 | + } | ||
| 56 | + }.title('pageOne').backgroundColor(Color.Pink) | ||
| 57 | + .onBackPressed(() => { | ||
| 58 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageOne onBackPress`) | ||
| 59 | + this.pageInfos.pop() | ||
| 60 | + return true | ||
| 61 | + }) | ||
| 62 | + .onWillAppear(()=>{ | ||
| 63 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillAppear") | ||
| 64 | + }) | ||
| 65 | + .onAppear(()=>{ | ||
| 66 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onAppear") | ||
| 67 | + }) | ||
| 68 | + .onWillShow(()=>{ | ||
| 69 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillShow") | ||
| 70 | + }) | ||
| 71 | + .onShown(()=>{ | ||
| 72 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onShown") | ||
| 73 | + }) | ||
| 74 | + .onWillHide(()=>{ | ||
| 75 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillHide") | ||
| 76 | + }) | ||
| 77 | + .onHidden(()=>{ | ||
| 78 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onHidden") | ||
| 79 | + }) | ||
| 80 | + .onWillDisappear(()=>{ | ||
| 81 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillDisappear") | ||
| 82 | + }) | ||
| 83 | + .onDisAppear(()=>{ | ||
| 84 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onDisAppear") | ||
| 85 | + }) | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + @Builder | ||
| 89 | + pageTwoTmp() { | ||
| 90 | + NavDestination() { | ||
| 91 | + Column({ space: 10 }) { | ||
| 92 | + } | ||
| 93 | + }.title('pageTwo').backgroundColor(Color.Orange) | ||
| 94 | + .onBackPressed(() => { | ||
| 95 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageTwo onBackPress`) | ||
| 96 | + this.pageInfos.pop() | ||
| 97 | + return true | ||
| 98 | + }) | ||
| 99 | + .onWillAppear(()=>{ | ||
| 100 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillAppear") | ||
| 101 | + }) | ||
| 102 | + .onAppear(()=>{ | ||
| 103 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onAppear") | ||
| 104 | + }) | ||
| 105 | + .onWillShow(()=>{ | ||
| 106 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillShow") | ||
| 107 | + }) | ||
| 108 | + .onShown(()=>{ | ||
| 109 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onShown") | ||
| 110 | + }) | ||
| 111 | + .onWillHide(()=>{ | ||
| 112 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillHide") | ||
| 113 | + }) | ||
| 114 | + .onHidden(()=>{ | ||
| 115 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onHidden") | ||
| 116 | + }) | ||
| 117 | + .onWillDisappear(()=>{ | ||
| 118 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillDisappear") | ||
| 119 | + }) | ||
| 120 | + .onDisAppear(()=>{ | ||
| 121 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onDisAppear") | ||
| 122 | + }) | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + @Builder | ||
| 126 | + pageThreeTmp() { | ||
| 127 | + NavDestination() { | ||
| 128 | + Column({ space: 10 }) { | ||
| 129 | + } | ||
| 130 | + }.title('pageThree') | ||
| 131 | + .mode(NavDestinationMode.DIALOG) | ||
| 132 | + .onBackPressed(() => { | ||
| 133 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageThree onBackPress`) | ||
| 134 | + this.pageInfos.pop() | ||
| 135 | + return true | ||
| 136 | + }) | ||
| 137 | + .onWillAppear(()=>{ | ||
| 138 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillAppear") | ||
| 139 | + }) | ||
| 140 | + .onAppear(()=>{ | ||
| 141 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onAppear") | ||
| 142 | + }) | ||
| 143 | + .onWillShow(()=>{ | ||
| 144 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillShow") | ||
| 145 | + }) | ||
| 146 | + .onShown(()=>{ | ||
| 147 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onShown") | ||
| 148 | + }) | ||
| 149 | + .onWillHide(()=>{ | ||
| 150 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillHide") | ||
| 151 | + }) | ||
| 152 | + .onHidden(()=>{ | ||
| 153 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onHidden") | ||
| 154 | + }) | ||
| 155 | + .onWillDisappear(()=>{ | ||
| 156 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onWillDisappear") | ||
| 157 | + }) | ||
| 158 | + .onDisAppear(()=>{ | ||
| 159 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageThree onDisAppear") | ||
| 160 | + }) | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + @Builder | ||
| 164 | + pageFourTmp() { | ||
| 165 | + NavDestination() { | ||
| 166 | + Column({ space: 10 }) { | ||
| 167 | + } | ||
| 168 | + }.title('pageFour') | ||
| 169 | + .mode(NavDestinationMode.DIALOG) | ||
| 170 | + .onBackPressed(() => { | ||
| 171 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageFour onBackPress`) | ||
| 172 | + this.pageInfos.pop() | ||
| 173 | + return true | ||
| 174 | + }) | ||
| 175 | + .onWillAppear(()=>{ | ||
| 176 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillAppear") | ||
| 177 | + }) | ||
| 178 | + .onAppear(()=>{ | ||
| 179 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onAppear") | ||
| 180 | + }) | ||
| 181 | + .onWillShow(()=>{ | ||
| 182 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillShow") | ||
| 183 | + }) | ||
| 184 | + .onShown(()=>{ | ||
| 185 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onShown") | ||
| 186 | + }) | ||
| 187 | + .onWillHide(()=>{ | ||
| 188 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillHide") | ||
| 189 | + }) | ||
| 190 | + .onHidden(()=>{ | ||
| 191 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onHidden") | ||
| 192 | + }) | ||
| 193 | + .onWillDisappear(()=>{ | ||
| 194 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onWillDisappear") | ||
| 195 | + }) | ||
| 196 | + .onDisAppear(()=>{ | ||
| 197 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageFour onDisAppear") | ||
| 198 | + }) | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + @Builder | ||
| 202 | + PageMap(name: string) { | ||
| 203 | + if (name === 'pageOne') { | ||
| 204 | + this.pageOneTmp() | ||
| 205 | + } else if (name === 'pageTwo') { | ||
| 206 | + this.pageTwoTmp() | ||
| 207 | + } else if (name === 'pageThree') { | ||
| 208 | + this.pageThreeTmp() | ||
| 209 | + } else if (name === 'pageFour') { | ||
| 210 | + this.pageFourTmp() | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + | ||
| 215 | + closeWindow() { | ||
| 216 | + console.log(`testTag prepare to close Window`) | ||
| 217 | + try { | ||
| 218 | + this.curWindow = window.findWindow('testWindow'); | ||
| 219 | + if (!this.curWindow) { | ||
| 220 | + console.log(`testTag failed to findWindow`); | ||
| 221 | + return; | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + this.curWindow.destroyWindow().then(() => { | ||
| 225 | + console.log(`testTag success to destroy curWindow`); | ||
| 226 | + }).catch((err: BusinessError) => { | ||
| 227 | + console.log(`failed to destroy curWindow`) | ||
| 228 | + }) | ||
| 229 | + } catch (e) { | ||
| 230 | + console.log(`testTag catch exception when find Window: ${JSON.stringify(e)}`) | ||
| 231 | + } | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + | ||
| 235 | + @State getAllPathName:string = '' | ||
| 236 | + | ||
| 237 | + | ||
| 238 | + build() { | ||
| 239 | + Column({space:5}){ | ||
| 240 | + Flex({ wrap: FlexWrap.Wrap, justifyContent:FlexAlign.Center }) { | ||
| 241 | + Button('关闭子窗口') | ||
| 242 | + .buttonStyles() | ||
| 243 | + .fontSize(12) | ||
| 244 | + .onClick(() => { | ||
| 245 | + this.closeWindow(); | ||
| 246 | + }) | ||
| 247 | + Button('push pageOne') | ||
| 248 | + .key('subWindowPageOne') | ||
| 249 | + .buttonStyles() | ||
| 250 | + .fontSize(12) | ||
| 251 | + .onClick(() => { | ||
| 252 | + this.pageInfos.pushPath({ name: 'pageOne' }) | ||
| 253 | + }) | ||
| 254 | + | ||
| 255 | + Button('push pageTwo') | ||
| 256 | + .key('subWindowPageTwo') | ||
| 257 | + .buttonStyles() | ||
| 258 | + .fontSize(12) | ||
| 259 | + .onClick(() => { | ||
| 260 | + this.pageInfos.pushPath({ name: 'pageTwo' }) | ||
| 261 | + }) | ||
| 262 | + | ||
| 263 | + Button('push pageThree') | ||
| 264 | + .key('subWindowPageThree') | ||
| 265 | + .buttonStyles() | ||
| 266 | + .fontSize(12) | ||
| 267 | + .onClick(() => { | ||
| 268 | + this.pageInfos.pushPath({ name: 'pageThree' }) | ||
| 269 | + }) | ||
| 270 | + | ||
| 271 | + Button('push pageFour') | ||
| 272 | + .key('subWindowPageFour') | ||
| 273 | + .buttonStyles() | ||
| 274 | + .fontSize(12) | ||
| 275 | + .onClick(() => { | ||
| 276 | + this.pageInfos.pushPath({ name: 'pageFour' }) | ||
| 277 | + }) | ||
| 278 | + | ||
| 279 | + Button('pop') | ||
| 280 | + .key('subWindowPop') | ||
| 281 | + .buttonStyles() | ||
| 282 | + .fontSize(12) | ||
| 283 | + .onClick(() => { | ||
| 284 | + this.pageInfos.pop() | ||
| 285 | + }) | ||
| 286 | + | ||
| 287 | + Button('replacePath pageTwo') | ||
| 288 | + .buttonStyles() | ||
| 289 | + .fontSize(12) | ||
| 290 | + .onClick(() => { | ||
| 291 | + this.pageInfos.replacePath({name: 'pageTwo', param: null}) | ||
| 292 | + }) | ||
| 293 | + | ||
| 294 | + Button('replacePath pageThree') | ||
| 295 | + .buttonStyles() | ||
| 296 | + .fontSize(12) | ||
| 297 | + .onClick(() => { | ||
| 298 | + this.pageInfos.replacePath({name: 'pageThree', param: null}) | ||
| 299 | + }) | ||
| 300 | + | ||
| 301 | + Button('replacePath pageFour') | ||
| 302 | + .buttonStyles() | ||
| 303 | + .fontSize(12) | ||
| 304 | + .onClick(() => { | ||
| 305 | + this.pageInfos.replacePath({name: 'pageFour', param: null}) | ||
| 306 | + }) | ||
| 307 | + | ||
| 308 | + Button('removeByIndexes [2]') | ||
| 309 | + .buttonStyles() | ||
| 310 | + .fontSize(12) | ||
| 311 | + .onClick(() => { | ||
| 312 | + this.pageInfos.removeByIndexes([2]) | ||
| 313 | + }) | ||
| 314 | + | ||
| 315 | + Button('clear') | ||
| 316 | + .key('subWindowClear') | ||
| 317 | + .buttonStyles() | ||
| 318 | + .fontSize(12) | ||
| 319 | + .onClick(() => { | ||
| 320 | + this.pageInfos.clear() | ||
| 321 | + }) | ||
| 322 | + | ||
| 323 | + Button('removeByIndexes [1]') | ||
| 324 | + .buttonStyles() | ||
| 325 | + .fontSize(12) | ||
| 326 | + .onClick(() => { | ||
| 327 | + this.pageInfos.removeByIndexes([1]) | ||
| 328 | + }) | ||
| 329 | + | ||
| 330 | + Button('pop*3') | ||
| 331 | + .buttonStyles() | ||
| 332 | + .fontSize(12) | ||
| 333 | + .onClick(() => { | ||
| 334 | + this.pageInfos.pop() | ||
| 335 | + this.pageInfos.pop() | ||
| 336 | + this.pageInfos.pop() | ||
| 337 | + }) | ||
| 338 | + | ||
| 339 | + Button('moveIndexToTop 0') | ||
| 340 | + .buttonStyles() | ||
| 341 | + .fontSize(12) | ||
| 342 | + .onClick(() => { | ||
| 343 | + this.pageInfos.moveIndexToTop(0) | ||
| 344 | + }) | ||
| 345 | + | ||
| 346 | + Button('moveIndexToTop 1') | ||
| 347 | + .buttonStyles() | ||
| 348 | + .fontSize(12) | ||
| 349 | + .onClick(() => { | ||
| 350 | + this.pageInfos.moveIndexToTop(1) | ||
| 351 | + }) | ||
| 352 | + | ||
| 353 | + Button('moveIndexToTop 2') | ||
| 354 | + .buttonStyles() | ||
| 355 | + .fontSize(12) | ||
| 356 | + .onClick(() => { | ||
| 357 | + this.pageInfos.moveIndexToTop(2) | ||
| 358 | + }) | ||
| 359 | + | ||
| 360 | + Button('popToIndex 0') | ||
| 361 | + .buttonStyles() | ||
| 362 | + .fontSize(12) | ||
| 363 | + .onClick(() => { | ||
| 364 | + this.pageInfos.popToIndex(0) | ||
| 365 | + }) | ||
| 366 | + } | ||
| 367 | + .width('100%').height('50%').borderWidth(1).borderColor('#ffe20ea6') | ||
| 368 | + | ||
| 369 | + Navigation(this.pageInfos) { | ||
| 370 | + Column({space:10}) { | ||
| 371 | + } | ||
| 372 | + } | ||
| 373 | + .borderWidth(1) | ||
| 374 | + .height('30%') | ||
| 375 | + .title('Navigation') | ||
| 376 | + .navDestination(this.PageMap) | ||
| 377 | + }.backgroundColor(Color.Pink) | ||
| 378 | + } | ||
| 379 | +} | ||
| @@ -0,0 +1,287 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import { hilog_log, SR_NO_MAP } from '../utils/Index'; | ||
| 16 | + | ||
| 17 | +@Entry | ||
| 18 | +@Component | ||
| 19 | +struct NavigationApiTest { | ||
| 20 | + @State pageInfos: NavPathStack = new NavPathStack(); | ||
| 21 | + | ||
| 22 | + onPageShow() { | ||
| 23 | + console.info('testTag pageA onPageShow'); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + onPageHide() { | ||
| 27 | + console.info('testTag pageA onPageHide'); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + onBackPress() { | ||
| 31 | + console.info('testTag pageA onBackPress'); | ||
| 32 | + return false | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + aboutToAppear() { | ||
| 36 | + console.info('testTag pageA aboutToAppear'); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + aboutToDisappear() { | ||
| 40 | + console.info('testTag pageA aboutToDisappear'); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @Builder | ||
| 44 | + pageOneTmp() { | ||
| 45 | + NavDestination() { | ||
| 46 | + Column({ space: 10 }) { | ||
| 47 | + } | ||
| 48 | + }.title('A').backgroundColor(Color.Pink) | ||
| 49 | + .onBackPressed(() => { | ||
| 50 | + return false | ||
| 51 | + }) | ||
| 52 | + .onWillAppear(()=>{ | ||
| 53 | + console.log("testTag A onWillAppear") | ||
| 54 | + }) | ||
| 55 | + .onAppear(()=>{ | ||
| 56 | + console.log("testTag A onAppear") | ||
| 57 | + }) | ||
| 58 | + .onWillShow(()=>{ | ||
| 59 | + console.log("testTag A onWillShow") | ||
| 60 | + }) | ||
| 61 | + .onShown(()=>{ | ||
| 62 | + console.log("testTag A onShown") | ||
| 63 | + }) | ||
| 64 | + .onWillHide(()=>{ | ||
| 65 | + console.log("testTag A onWillHide") | ||
| 66 | + }) | ||
| 67 | + .onHidden(()=>{ | ||
| 68 | + console.log("testTag A onHidden") | ||
| 69 | + }) | ||
| 70 | + .onWillDisappear(()=>{ | ||
| 71 | + console.log("testTag A onWillDisappear") | ||
| 72 | + }) | ||
| 73 | + .onDisAppear(()=>{ | ||
| 74 | + console.log("testTag A onDisAppear") | ||
| 75 | + }) | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Builder | ||
| 79 | + pageTwoTmp() { | ||
| 80 | + NavDestination() { | ||
| 81 | + Column({ space: 10 }) { | ||
| 82 | + } | ||
| 83 | + }.title('B').backgroundColor(Color.Yellow) | ||
| 84 | + .onBackPressed(() => { | ||
| 85 | + return false | ||
| 86 | + }) | ||
| 87 | + .onWillAppear(()=>{ | ||
| 88 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onWillAppear") | ||
| 89 | + }) | ||
| 90 | + .onAppear(()=>{ | ||
| 91 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onAppear") | ||
| 92 | + }) | ||
| 93 | + .onWillShow(()=>{ | ||
| 94 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onWillShow") | ||
| 95 | + }) | ||
| 96 | + .onShown(()=>{ | ||
| 97 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onShown") | ||
| 98 | + }) | ||
| 99 | + .onWillHide(()=>{ | ||
| 100 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onWillHide") | ||
| 101 | + }) | ||
| 102 | + .onHidden(()=>{ | ||
| 103 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onHidden") | ||
| 104 | + }) | ||
| 105 | + .onWillDisappear(()=>{ | ||
| 106 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onWillDisappear") | ||
| 107 | + }) | ||
| 108 | + .onDisAppear(()=>{ | ||
| 109 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag B onDisAppear") | ||
| 110 | + }) | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + | ||
| 114 | + @Builder | ||
| 115 | + pageThreeTmp() { | ||
| 116 | + NavDestination() { | ||
| 117 | + Column({ space: 10 }) { | ||
| 118 | + } | ||
| 119 | + }.title('C') | ||
| 120 | + .onBackPressed(() => { | ||
| 121 | + return false | ||
| 122 | + }) | ||
| 123 | + .onWillAppear(()=>{ | ||
| 124 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onWillAppear") | ||
| 125 | + }) | ||
| 126 | + .onAppear(()=>{ | ||
| 127 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onAppear") | ||
| 128 | + }) | ||
| 129 | + .onWillShow(()=>{ | ||
| 130 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onWillShow") | ||
| 131 | + }) | ||
| 132 | + .onShown(()=>{ | ||
| 133 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onShown") | ||
| 134 | + }) | ||
| 135 | + .onWillHide(()=>{ | ||
| 136 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onWillHide") | ||
| 137 | + }) | ||
| 138 | + .onHidden(()=>{ | ||
| 139 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onHidden") | ||
| 140 | + }) | ||
| 141 | + .onWillDisappear(()=>{ | ||
| 142 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onWillDisappear") | ||
| 143 | + }) | ||
| 144 | + .onDisAppear(()=>{ | ||
| 145 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag C onDisAppear") | ||
| 146 | + }) | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + | ||
| 150 | + @Builder | ||
| 151 | + PageMap(name: string) { | ||
| 152 | + if (name === 'A') { | ||
| 153 | + this.pageOneTmp() | ||
| 154 | + } else if (name === 'B') { | ||
| 155 | + this.pageTwoTmp() | ||
| 156 | + } else if (name === 'C') { | ||
| 157 | + this.pageThreeTmp() | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + | ||
| 162 | + | ||
| 163 | + build() { | ||
| 164 | + Column({space:5}){ | ||
| 165 | + Button('push A') | ||
| 166 | + .onClick(() => { | ||
| 167 | + this.pageInfos.pushPath({ name: 'A' }) | ||
| 168 | + }) | ||
| 169 | + | ||
| 170 | + Button('push B') | ||
| 171 | + .onClick(() => { | ||
| 172 | + this.pageInfos.pushPath({ name: 'B' }) | ||
| 173 | + }) | ||
| 174 | + | ||
| 175 | + Button('push C') | ||
| 176 | + .onClick(() => { | ||
| 177 | + this.pageInfos.pushPath({ name: 'C' }) | ||
| 178 | + }) | ||
| 179 | + | ||
| 180 | + Button('pop') | ||
| 181 | + .onClick(() => { | ||
| 182 | + this.pageInfos.pop() | ||
| 183 | + }) | ||
| 184 | + | ||
| 185 | + Button('clear') | ||
| 186 | + .onClick(() => { | ||
| 187 | + this.pageInfos.clear() | ||
| 188 | + }) | ||
| 189 | + | ||
| 190 | + Button(`跳转到B,调用didshow,跳出弹窗,pushPath到C`, { stateEffect: true, type: ButtonType.Capsule }) | ||
| 191 | + .key('didShowToB') | ||
| 192 | + .width('80%') | ||
| 193 | + .height(40) | ||
| 194 | + .onClick(() => { | ||
| 195 | + this.pageInfos.setInterception({ | ||
| 196 | + willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar", operation: NavigationOperation, animated: boolean) => { | ||
| 197 | + console.log(`will show`) | ||
| 198 | + }, | ||
| 199 | + didShow:(from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar", operation: NavigationOperation, animated: boolean) => { | ||
| 200 | + if (typeof to === "string") { | ||
| 201 | + console.log("target page is navigation home"); | ||
| 202 | + return; | ||
| 203 | + } | ||
| 204 | + let target: NavDestinationContext = to as NavDestinationContext; | ||
| 205 | + if (target.pathInfo.name === 'B') { | ||
| 206 | + target.pathStack.pop(); | ||
| 207 | + AlertDialog.show( | ||
| 208 | + { | ||
| 209 | + title: '弹窗', | ||
| 210 | + message: 'text', | ||
| 211 | + autoCancel: true, | ||
| 212 | + alignment: DialogAlignment.Bottom, | ||
| 213 | + offset: { dx: 0, dy: -20 }, | ||
| 214 | + gridCount: 3, | ||
| 215 | + confirm: { | ||
| 216 | + value: '确认', | ||
| 217 | + action: () => { | ||
| 218 | + target.pathStack.pushPath({ name:'C' }) | ||
| 219 | + console.info('Button-clicking callback') | ||
| 220 | + } | ||
| 221 | + } | ||
| 222 | + } | ||
| 223 | + ) | ||
| 224 | + | ||
| 225 | + } | ||
| 226 | + console.log(`did show`) | ||
| 227 | + } | ||
| 228 | + }); | ||
| 229 | + | ||
| 230 | + this.pageInfos.pushPath({name:'B'}) | ||
| 231 | + }) | ||
| 232 | + | ||
| 233 | + Button(`跳转到B,调用willShow,跳出弹窗,pushPath到C`, { stateEffect: true, type: ButtonType.Capsule }) | ||
| 234 | + .key('willShowToB') | ||
| 235 | + .width('80%') | ||
| 236 | + .height(40) | ||
| 237 | + .onClick(() => { | ||
| 238 | + this.pageInfos.setInterception({ | ||
| 239 | + willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar", operation: NavigationOperation, animated: boolean) => { | ||
| 240 | + if (typeof to === "string") { | ||
| 241 | + console.log("target page is navigation home"); | ||
| 242 | + return; | ||
| 243 | + } | ||
| 244 | + let target: NavDestinationContext = to as NavDestinationContext; | ||
| 245 | + if (target.pathInfo.name === 'B') { | ||
| 246 | + target.pathStack.pop(); | ||
| 247 | + AlertDialog.show( | ||
| 248 | + { | ||
| 249 | + title: '弹窗', | ||
| 250 | + message: 'text', | ||
| 251 | + autoCancel: true, | ||
| 252 | + alignment: DialogAlignment.Bottom, | ||
| 253 | + offset: { dx: 0, dy: -20 }, | ||
| 254 | + gridCount: 3, | ||
| 255 | + confirm: { | ||
| 256 | + value: '确认', | ||
| 257 | + action: () => { | ||
| 258 | + target.pathStack.pushPath({ name:'C' }) | ||
| 259 | + console.info('Button-clicking callback') | ||
| 260 | + } | ||
| 261 | + }, | ||
| 262 | + } | ||
| 263 | + ) | ||
| 264 | + | ||
| 265 | + } | ||
| 266 | + console.log(`will show`) | ||
| 267 | + }, | ||
| 268 | + didShow:(from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar", operation: NavigationOperation, animated: boolean) => { | ||
| 269 | + console.log(`did show`) | ||
| 270 | + } | ||
| 271 | + }); | ||
| 272 | + this.pageInfos.pushPath({name:'B'}) | ||
| 273 | + }) | ||
| 274 | + | ||
| 275 | + Navigation(this.pageInfos) { | ||
| 276 | + Column({space:10}) { | ||
| 277 | + } | ||
| 278 | + } | ||
| 279 | + .borderWidth(1) | ||
| 280 | + .height('30%') | ||
| 281 | + .title('Navigation') | ||
| 282 | + .navDestination(this.PageMap) | ||
| 283 | + | ||
| 284 | + } | ||
| 285 | + | ||
| 286 | + } | ||
| 287 | +} | ||
Aexamples/Navigation/entry/src/main/ets/pages/NavigationLifeCycle/customNavContentTransition.ets+264-0
| @@ -0,0 +1,264 @@ | |||
| 1 | +/* | ||
| 2 | + * Copyright (c) 2026 Huawei Device Co., Ltd. | ||
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | + * you may not use this file except in compliance with the License. | ||
| 5 | + * You may obtain a copy of the License at | ||
| 6 | + * | ||
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | + * | ||
| 9 | + * Unless required by applicable law or agreed to in writing, software | ||
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | + * See the License for the specific language governing permissions and | ||
| 13 | + * limitations under the License. | ||
| 14 | + */ | ||
| 15 | +import { CustomTransition } from '../utils/CustomNavigationUtils' | ||
| 16 | +import { hilog_log, SR_NO_MAP } from '../utils/Index'; | ||
| 17 | + | ||
| 18 | +let durationOne: number = 1000 | ||
| 19 | +let durationTwo: number = 1000 | ||
| 20 | + | ||
| 21 | +@Entry | ||
| 22 | +@Component | ||
| 23 | +struct NavigationExample2 { | ||
| 24 | + @State timeout: number = 1000 | ||
| 25 | + @State routerTo: string = '' | ||
| 26 | + @State routerFrom: string = '' | ||
| 27 | + @State proxyTo: string = '' | ||
| 28 | + @State proxyFrom: string = '' | ||
| 29 | + @State mode: NavigationMode = NavigationMode.Stack | ||
| 30 | + @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack() | ||
| 31 | + | ||
| 32 | + @Builder | ||
| 33 | + PageMap(name: string) { | ||
| 34 | + if (name === 'pageOne') { | ||
| 35 | + pageOne() | ||
| 36 | + } else if (name === 'pageTwo') { | ||
| 37 | + pageTwo() | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + build() { | ||
| 42 | + Column({space:5}) { | ||
| 43 | + Column({space:5}){ | ||
| 44 | + Button('push pageOne') | ||
| 45 | + .onClick(() => { | ||
| 46 | + this.pageInfos.pushPath({ name: 'pageOne' }) | ||
| 47 | + }).margin(5) | ||
| 48 | + | ||
| 49 | + Button('push pageTwo') | ||
| 50 | + .onClick(() => { | ||
| 51 | + this.pageInfos.pushPath({ name: 'pageTwo' }) | ||
| 52 | + }).margin(5) | ||
| 53 | + | ||
| 54 | + Button('pop') | ||
| 55 | + .onClick(() => { | ||
| 56 | + this.pageInfos.pop() | ||
| 57 | + }).margin(5) | ||
| 58 | + | ||
| 59 | + Button('clear') | ||
| 60 | + .onClick(() => { | ||
| 61 | + this.pageInfos.clear() | ||
| 62 | + }).margin(5) | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + Navigation(this.pageInfos) { | ||
| 67 | + Column({space:10}) { | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + .borderWidth(1) | ||
| 71 | + .height('30%') | ||
| 72 | + .title('Navigation121212') | ||
| 73 | + .navDestination(this.PageMap) | ||
| 74 | + .customNavContentTransition((from: NavContentInfo, to: NavContentInfo, operation: NavigationOperation) => { | ||
| 75 | + if (from.mode === NavDestinationMode.DIALOG || to.mode === NavDestinationMode.DIALOG) { | ||
| 76 | + console.log("Dialog navDestination do system transition animation.") | ||
| 77 | + return undefined | ||
| 78 | + } | ||
| 79 | + this.routerTo = JSON.stringify(to) + '' | ||
| 80 | + this.routerFrom = JSON.stringify(from) + '' | ||
| 81 | + // 其他页面做自定义动画 | ||
| 82 | + let customAnimation: NavigationAnimatedTransition | ||
| 83 | + customAnimation = { | ||
| 84 | + onTransitionEnd: (success) => { | ||
| 85 | + if (success) { | ||
| 86 | + console.log("custom transition success"); | ||
| 87 | + } else { | ||
| 88 | + console.log("custom transition failed"); | ||
| 89 | + } | ||
| 90 | + }, | ||
| 91 | + // timeout: this.timeout, | ||
| 92 | + transition: (transitionContext) => { | ||
| 93 | + this.proxyTo = JSON.stringify(transitionContext.to) + '' | ||
| 94 | + this.proxyFrom = JSON.stringify(transitionContext.from) + '' | ||
| 95 | + let onFinish: () => void = () => { | ||
| 96 | + transitionContext.finishTransition() | ||
| 97 | + } | ||
| 98 | + // from页面的自定义动画 | ||
| 99 | + let fromAnimatedFunc: (isPush: boolean, onFinish: () => void,isexit: boolean, isPop: boolean) => void | ||
| 100 | + fromAnimatedFunc = CustomTransition.getInstance().getNavParam(transitionContext.from.name) | ||
| 101 | + if (fromAnimatedFunc != undefined) { | ||
| 102 | + fromAnimatedFunc(operation === NavigationOperation.PUSH, onFinish,false,false) | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + // to页面的自定义动画 | ||
| 106 | + let toAnimatedFunc: (isPush: boolean, onFinish: () => void,isexit: boolean, isPop: boolean) => void | ||
| 107 | + toAnimatedFunc = CustomTransition.getInstance().getNavParam(transitionContext.to.name) | ||
| 108 | + if (toAnimatedFunc != undefined) { | ||
| 109 | + toAnimatedFunc(operation === NavigationOperation.PUSH, onFinish,true,false) | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + return customAnimation | ||
| 114 | + }) | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + } | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +@Component | ||
| 122 | +export struct pageOne { | ||
| 123 | + @Consume('pageInfos') pageInfos: NavPathStack; | ||
| 124 | + @State opacity1: number = 1 | ||
| 125 | + @State duration: number = durationOne | ||
| 126 | + | ||
| 127 | + aboutToAppear() { | ||
| 128 | + CustomTransition.getInstance().registerNavParam('pageOne', (isPush: boolean, onFinish: () => void,isexit:boolean) => { | ||
| 129 | + if (isexit) { | ||
| 130 | + this.opacity1 = 0 | ||
| 131 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 132 | + this.opacity1 = 1 | ||
| 133 | + }) | ||
| 134 | + } else if (isPush) { | ||
| 135 | + // push 转场 | ||
| 136 | + this.opacity1 = 0 | ||
| 137 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 138 | + this.opacity1 = 1 | ||
| 139 | + }) | ||
| 140 | + } else { | ||
| 141 | + // pop 转场 | ||
| 142 | + this.opacity1 = 1 | ||
| 143 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 144 | + this.opacity1 = 0 | ||
| 145 | + }) | ||
| 146 | + } | ||
| 147 | + }) | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + aboutToDisappear() { | ||
| 151 | + CustomTransition.getInstance().unRegisterNavParam('pageOne') | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + build() { | ||
| 155 | + NavDestination() { | ||
| 156 | + Column() { | ||
| 157 | + }.width('100%').height('100%') | ||
| 158 | + }.title('pageOne').opacity(this.opacity1) | ||
| 159 | + .onBackPressed(() => { | ||
| 160 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageOne onBackPress`) | ||
| 161 | + this.pageInfos.pop() | ||
| 162 | + return true | ||
| 163 | + }) | ||
| 164 | + .onWillAppear(()=>{ | ||
| 165 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillAppear") | ||
| 166 | + }) | ||
| 167 | + .onAppear(()=>{ | ||
| 168 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onAppear") | ||
| 169 | + }) | ||
| 170 | + .onWillShow(()=>{ | ||
| 171 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillShow") | ||
| 172 | + }) | ||
| 173 | + .onShown(()=>{ | ||
| 174 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onShown") | ||
| 175 | + }) | ||
| 176 | + .onWillHide(()=>{ | ||
| 177 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillHide") | ||
| 178 | + }) | ||
| 179 | + .onHidden(()=>{ | ||
| 180 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onHidden") | ||
| 181 | + }) | ||
| 182 | + .onWillDisappear(()=>{ | ||
| 183 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onWillDisappear") | ||
| 184 | + }) | ||
| 185 | + .onDisAppear(()=>{ | ||
| 186 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageOne onDisAppear") | ||
| 187 | + }) | ||
| 188 | + } | ||
| 189 | +} | ||
| 190 | + | ||
| 191 | + | ||
| 192 | +@Component | ||
| 193 | +export struct pageTwo { | ||
| 194 | + @Consume('pageInfos') pageInfos: NavPathStack; | ||
| 195 | + @State translateX: number = 0 | ||
| 196 | + @State opacity1: number = 1 | ||
| 197 | + @State duration: number = durationTwo | ||
| 198 | + | ||
| 199 | + aboutToAppear() { | ||
| 200 | + CustomTransition.getInstance().registerNavParam('pageTwo', (isPush: boolean, onFinish: () => void,isexit:boolean) => { | ||
| 201 | + if (isexit) { | ||
| 202 | + this.opacity1 = 0 | ||
| 203 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 204 | + this.opacity1 = 1 | ||
| 205 | + }) | ||
| 206 | + } else if (isPush) { | ||
| 207 | + // push 转场 | ||
| 208 | + this.opacity1 = 0 | ||
| 209 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 210 | + this.opacity1 = 1 | ||
| 211 | + }) | ||
| 212 | + } else { | ||
| 213 | + // pop 转场 | ||
| 214 | + this.opacity1 = 1 | ||
| 215 | + animateTo({ duration: durationOne, onFinish: onFinish }, () => { | ||
| 216 | + this.opacity1 = 0 | ||
| 217 | + }) | ||
| 218 | + } | ||
| 219 | + }) | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + aboutToDisappear() { | ||
| 223 | + CustomTransition.getInstance().unRegisterNavParam('pageTwo') | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + build() { | ||
| 227 | + Scroll() { | ||
| 228 | + NavDestination() { | ||
| 229 | + Column() { | ||
| 230 | + }.width('100%').height('100%') | ||
| 231 | + }.title('pageTwo').opacity(this.opacity1) | ||
| 232 | + .onBackPressed(() => { | ||
| 233 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, `testTag pageTwo onBackPress`) | ||
| 234 | + this.pageInfos.pop() | ||
| 235 | + return true | ||
| 236 | + }) | ||
| 237 | + .onWillAppear(()=>{ | ||
| 238 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillAppear") | ||
| 239 | + }) | ||
| 240 | + .onAppear(()=>{ | ||
| 241 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onAppear") | ||
| 242 | + }) | ||
| 243 | + .onWillShow(()=>{ | ||
| 244 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillShow") | ||
| 245 | + }) | ||
| 246 | + .onShown(()=>{ | ||
| 247 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onShown") | ||
| 248 | + }) | ||
| 249 | + .onWillHide(()=>{ | ||
| 250 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillHide") | ||
| 251 | + }) | ||
| 252 | + .onHidden(()=>{ | ||
| 253 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onHidden") | ||
| 254 | + }) | ||
| 255 | + .onWillDisappear(()=>{ | ||
| 256 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onWillDisappear") | ||
| 257 | + }) | ||
| 258 | + .onDisAppear(()=>{ | ||
| 259 | + hilog_log(SR_NO_MAP.NavigationLifeCycle, "testTag pageTwo onDisAppear") | ||
| 260 | + }) | ||
| 261 | + } | ||
| 262 | + | ||
| 263 | + } | ||
| 264 | +} | ||