提供类似aar包内Activity之间跳转问题?

问题描述

  1. SDK对外提供接口,启动接口会拉起SDK内部的页面。
  2. SDK内部有多个页面,页面间有来回的跳转。
阅读 209
1 个回答

解决措施

可以在har中使用命名路由( router.pushNamedRoute )来实现

示例代码

MainPage

@Entry({ routeName: 'MainPage' }) 
@Component 
export struct MainPage { 
  @State message: string = 'Hello World'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
          .backgroundColor(Color.Pink) 
        Text(JSON.stringify(router.getParams())) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
          .backgroundColor(Color.Yellow) 
        Button('跳转第二个页面') 
          .onClick(() => { 
            router.pushNamedRoute({ 
              name: 'SecondPage' 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

SecondPage

@Entry({ routeName: 'SecondPage' }) 
@Component 
export struct SecondPage { 
  @State message: string = 'SecondPage'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .backgroundColor(Color.Yellow) 
        Button('返回上一页router') 
          .onClick(() => { 
            console.log('返回上一页通过router') 
            router.pushNamedRoute({ 
              name: 'MainPage' 
            }) 
          }) 
        Button('返回上一页通过back') 
          .onClick(() => { 
            console.log('返回上一页通过back') 
            router.back() 
 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
class RouterParams { 
  private param1: string 
  private param2: number[] 
 
  constructor(param1: string, param2: number[]) { 
    this.param1 = param1 
    this.param2 = param2 
  } 
} 
 
export class Test { 
  static routerToAnother(message: string, arr: Array<number>) { 
    router.pushNamedRoute({ 
      name: 'MainPage', 
      params: new RouterParams(message, arr) 
    }) 
  } 
}

har中Index.ets

export { MainPage } from './src/main/ets/components/mainpage/MainPage'; 
export { Test } from './src/main/ets/components/mainpage/Test'; 
export {SecondPage}from'./src/main/ets/components/mainpage/SecondPage'

外部引用

@Entry 
@Component 
struct Index { 
  @State message: string = '使用接口方法'; 
 
  build() { 
    Row() { 
      Column() { 
        Button(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            Test.routerToAnother('message', [1, 2.3]) 
          }) 
 
        Button('使用命名路由') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            router.pushNamedRoute({ 
              name: 'MainPage' 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进