HarmonyOS 热启动如何判断某个页面存在

比如现在APP打开了三个页面A->B->C,现在把应用滑到后台,通过桌面上的服务卡片拉起应用,想在onNewWant通过参数跳转到指定页面,例如上方的B页面,该如何判断是否打开了B页面,如果开打了,需要关闭B并重新打开。

HarmonyOS
2024-08-29 11:37:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

如果用的router页面路由,目前无路由管理功能。Navigation目前支持页面管理。比如接口getAllPathName()和moveIndexToTop()。具体文档如下https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#getallpathname10

router虽然没有页面管理,但是有一个RouterMode,路由跳转模式。其中的Single模式,应该可以满足需求:单实例模式,如果目标页面的url已经存在于页面栈中,则该url页面移动到栈顶。如果目标页面的url在页面栈中不存在同url页面,则按照默认的多实例模式进行跳转。其文档如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-router-V5#routermode9

//热启动,跳转到具体到页面 
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 
  console.log(`onNewWant, want: ${want.abilityName}`); 
  window.getLastWindow(getContext(this), (error, topWindow) => { 
    if (topWindow) { 
      console.log(`onNewWant, want: ${JSON.stringify(want.parameters)}`); 
      let param =  want.parameters as Record<string, Object>; 
      let routerStr = param.message as string 
      if(routerStr != undefined){ 
        let typeTemp = param.type as string//设置数据来源类型,便于跳转后的处理判断 
        router.pushUrl({ 
          url: routerStr, // 目标url 
          params: { 
            type: typeTemp 
          } 
        }, router.RouterMode.Single, (err) => { 
          if (err) { 
            console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`); 
            return; 
          } 
          console.info('Invoke pushUrl succeeded.'); 
        }); 
 
      } 
 
    } 
  }); 
}

卡片ets中:

FormLink({ 
  action: 'router', 
  abilityName: 'EntryAbility', 
  params: { 
    message: 'pages/searchSwiperPage', 
    type :'widgetCard' 
  } 
}) 

这边提供一个demo,参考一下:首先Index.ets页面。

import { A } from '../components/A'; 
import { B } from '../components/B'; 
import { C } from '../components/C'; 
import { RouterModule } from '../utils/routerModule'; 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello Index'; 
  @State entryRouter: NavPathStack = new NavPathStack(); 
  aboutToAppear(): void { 
    if (!this.entryRouter) { 
      this.entryRouter = new NavPathStack(); 
    } 
    RouterModule.setRouterStack(this.entryRouter); 
  } 
  @Builder 
  routerMap(builderName: string, param: object) { 
    if (builderName === 'pageA') { 
      A(); 
    } else if (builderName === 'pageB') { 
      B(); 
    } else if (builderName === 'pageC') { 
      C(); 
    }else { 
      // Obtain the WrappedBuilder object based on the module name, create a page through the builder interface, and import the param parameter. 
    } 
  }; 
  build() { 
    Navigation(this.entryRouter) { 
      Column() { 
        Button('跳转pageA') 
          .onClick(() => { 
            const pathInfo = new NavPathInfo('pageA', null); 
            RouterModule.getRouterStatic().pushPath(pathInfo); 
          }) 
      } 
      .width('100%') 
    } 
    .navDestination(this.routerMap); 
  } 
}

接着routerModule.ets页面。

export class RouterModule { 
  private static routerStack: NavPathStack; 
 
  static setRouterStack(pathStack: NavPathStack) { 
    RouterModule.routerStack = pathStack; 
  } 
 
  static getRouterStatic() { 
    return RouterModule.routerStack; 
  } 
}

然后A.ets页面。

import { RouterModule } from '../utils/routerModule'; 
@Component 
export struct A{ 
  build() { 
    NavDestination() { 
      Text('PageA') 
      Button('跳转pageB') 
        .onClick(() => { 
          const pathInfo = new NavPathInfo('pageB', null); 
          RouterModule.getRouterStatic().pushPath(pathInfo); 
        }) 
    } 
  } 
}

然后B.ets页面。

import { RouterModule } from '../utils/routerModule'; 
@Component 
export struct B{ 
  build() { 
    NavDestination() { 
      Text('PageB') 
      Button('跳转pageC') 
        .onClick(() => { 
          const pathInfo = new NavPathInfo('pageC', null); 
          RouterModule.getRouterStatic().pushPath(pathInfo); 
        }) 
    } 
  } 
}

再然后C.ets页面。

@Component 
export struct C{ 
  build() { 
    NavDestination() { 
      Text('PageC') 
    } 
  } 
}

最后EntryAbility .ets ,里面主要关注RouterModule 导入和onNewWant。

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
import { window } from '@kit.ArkUI'; 
import { RouterModule } from '../utils/routerModule'; 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
    windowStage.loadContent('pages/Index', (err) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); 
    }); 
  } 
  //热启动,跳转到具体到页面 
  onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 
    console.log(`onNewWant, want: ${want.abilityName}`); 
    const pathStack = RouterModule.getRouterStatic(); 
    let param =  want.parameters as Record<string, Object>; 
    let backPage = param.message as string 
    const isPageBInStack = pathStack.getAllPathName().indexOf(backPage) !== -1; 
    console.log(`onNewWant ${backPage} is in pathStack: ${isPageBInStack}`); 
    if (isPageBInStack) { 
      // pathStack.moveToTop(backPage); 
      pathStack.removeByName(backPage); 
      const pathInfo = new NavPathInfo(backPage, null); 
      pathStack.pushPath(pathInfo); 
    }else { 
      const pathInfo = new NavPathInfo(backPage, null); 
      pathStack.pushPath(pathInfo); 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-29 16:53:02
相关问题
如何判断JS对象中是否存在某个
2198浏览 • 1回复 待解决
UIAbility热启动的过程是怎样的?
829浏览 • 1回复 待解决
HarmonyOS如何判断指定应用是否存在
331浏览 • 1回复 待解决
如何判断对象是某个interface的实现
963浏览 • 1回复 待解决
HarmonyOS判断Object中是否含有某个属性
657浏览 • 1回复 待解决
怎么判断某个customDialog是否弹出?
182浏览 • 1回复 待解决
如何判断某个应用是否为系统应用
2114浏览 • 1回复 待解决
PostgreSQL insert 前判断是否存在
4105浏览 • 2回复 待解决
如何判断一个对象实现了某个接口
1252浏览 • 1回复 待解决
如何指定首次应用的启动页面
534浏览 • 1回复 待解决
如何NavPathStack也支持页面启动模式
419浏览 • 1回复 待解决