HarmonyOS 首页组件生命周期问题

首页启动后通过Navigation打开或关闭其他页面,Tabs容器下子视图没有收到生命周期回调。

HarmonyOS
2025-01-09 17:07:33
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

参考示例如下:

//index.ets
@Entry
@Component
struct NavigationExample {
  onPageShow(): void {
    this.isPageVisible = true
    console.log("--NavigationOnPageShow")
  }

  onPageHide(): void {
    this.isPageVisible = false
    console.log("--NavigationOnPageHide")
  }

  @State isPageVisible: boolean = false;
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack();

  @Builder
  PageMap(name: string) {
    if (name === "NavDestinationTitle1") {
      pageOneTmp()
    } else if (name === "NavDestinationTitle2") {
      pageTwoTmp()
    }
  }

  build() {
    Column() {
      Navigation(this.pageInfos) {
        TextInput({ placeholder: 'search...' })
          .width("90%")
          .height(40)
          .backgroundColor('#FFFFFF')
        Tabs() {
          TabContent() {
            SourceVideoPage({ isPageVisible: this.isPageVisible })
          }
          .tabBar('首页')

          TabContent() {
            Text('推荐的内容').fontSize(30)
          }
          .tabBar('推荐')
        }

      }
      .title("主标题")
      .mode(NavigationMode.Stack)
      .navDestination(this.PageMap)
      .onNavBarStateChange((isVisible: boolean) => {
        console.info('-->isVisible:' + isVisible)
        if (isVisible) {
          this.isPageVisible = true
        } else {
          this.isPageVisible = false
        }
      })
    }
    .height('100%')
    .width('100%')
    .backgroundColor('#F1F3F5')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
// PageOne.ets
@Component
export struct pageOneTmp {
  @Consume('pageInfos') pageInfos: NavPathStack;
  private arr: number[] = [1, 2];

  build() {
    NavDestination() {
      Column() {
        Text("NavDestinationContent1")
        List({ space: 12 }) {
          ForEach(this.arr, (item: string) => {
            ListItem() {
              Text("NavRouter" + item)
                .width("100%")
                .height(72)
                .backgroundColor('#FFFFFF')
                .borderRadius(24)
                .fontSize(16)
                .fontWeight(500)
                .textAlign(TextAlign.Center)
                .onClick(() => {
                  this.pageInfos.pushPath({ name: "NavDestinationTitle" + item })
                })
            }
          }, (item: string): string => item)
        }
        .width("90%")
        .margin({ top: 12 })
      }.width('100%').height('100%')
    }
    .title("NavDestinationTitle1")
    .onBackPressed(() => {
      const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
      console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
      return true
    })
    .onShown(() => {
      console.log("--pageOneTmpOnShown")
    })
    .onHidden(() => {
      console.log("--pageOneTmpOnHidden")
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
// PageTwo.ets
@Component
export struct pageTwoTmp {
  @Consume('pageInfos') pageInfos: NavPathStack;

  build() {
    NavDestination() {
      Column() {
        Text("NavDestinationContent2")
      }.width('100%').height('100%')
    }.title("NavDestinationTitle2")
    .onBackPressed(() => {
      const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素
      console.log('pop' + '返回值' + JSON.stringify(popDestinationInfo))
      return true
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
@Component
export struct SourceVideoPage {
  @Prop @Watch('onVideoChange') isPageVisible: boolean;
  controller: VideoController = new VideoController();
  @Consume('pageInfos') pageInfos: NavPathStack;
  private arr: number[] = [1, 2];

  onVideoChange() {
    if (this.isPageVisible) {
      this.controller.start()
    } else {
      this.controller.pause()
    }
  }

  build() {
    Column() {
      Video({
        src: $rawfile('videoTest.mp4'),
        controller: this.controller
      })
        .height(220)
        .width('90%')
        .loop(true)
        .onPrepared((event) => {
          this.controller.start();
          console.info(`--video ${event.duration}`)
        })
        .onStart(() => {
          console.info(`--video start`)
        })
        .onPause(() => {
          console.info(`--video pause`)
        })
      List({ space: 12 }) {
        ForEach(this.arr, (item: string) => {
          ListItem() {
            Text("NavRouter" + item)
              .width("100%")
              .height(72)
              .backgroundColor('#FFFFFF')
              .borderRadius(24)
              .fontSize(16)
              .fontWeight(500)
              .textAlign(TextAlign.Center)
              .onClick(() => {
                this.pageInfos.pushPath({ name: "NavDestinationTitle" + item })
              })
          }
        }, (item: string): string => item)
      }
      .width("90%")
    }
    .height("100%")
    .width("100%")
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
分享
微博
QQ
微信
回复
2025-01-09 18:56:44
相关问题
Dialog组件生命周期问题
950浏览 • 1回复 待解决
HarmonyOS tab组件生命周期问题
555浏览 • 1回复 待解决
HarmonyOS Navigation生命周期问题
617浏览 • 1回复 待解决
HarmonyOS NavDestination生命周期问题
517浏览 • 1回复 待解决
HarmonyOS 页面生命周期问题
698浏览 • 1回复 待解决
HarmonyOS 自定义组件生命周期
745浏览 • 1回复 待解决
Window窗口的生命周期问题
1009浏览 • 1回复 待解决
HarmonyOS Navigation 生命周期
514浏览 • 1回复 待解决
HarmonyOS Navigation生命周期
792浏览 • 1回复 待解决
HarmonyOS 关于ListItem的生命周期问题
378浏览 • 1回复 待解决
如何知晓navigation组件生命周期
791浏览 • 1回复 待解决
HarmonyOS Navigation跳转的组件生命周期
1687浏览 • 2回复 待解决
监听Ability生命周期
1941浏览 • 1回复 待解决
HarmonyOS 弹框Dialog的生命周期问题
1105浏览 • 1回复 待解决
HarmonyOS 自定义生命周期问题
593浏览 • 1回复 待解决
HarmonyOS 模块生命周期管理
961浏览 • 1回复 待解决
HarmonyOS 生命周期的区别
826浏览 • 1回复 待解决
弹窗组件无法调用生命周期接口
2851浏览 • 1回复 待解决