You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否可通过Navigation Component DeepLink传递Integer Array整型数组参数

解决方案

Android的Uri本身仅支持字符串类型的参数传递,Navigation组件的DeepLink无法直接识别整型数组类型的路径参数,你可以按以下步骤实现:

  • 构造DeepLink Uri时,将整型数组的所有元素用英文逗号拼接为单个字符串作为参数值
    示例:如果要传递的数组为[1001, 1002, 1003],最终Uri格式如下:
    your_scheme://your_host/target_path?references=1001,1002,1003
    
  • 如果需要适配Navigation的DeepLink自动解析逻辑,可以将你现有的参数定义的argType临时改为string,配套DeepLink配置如下:
    <deepLink
        app:uri="your_scheme://your_host/target_path?references={references}" />
    <argument
            android:name="references"
            app:argType="string"
            app:nullable="false" />
    
  • 目标页接收参数时,先拿到该字符串参数,按逗号分割为字符串数组后遍历转换为Int类型即可
    Kotlin示例代码:
    // 未使用Safe Args的场景
    val referencesStr = intent?.data?.getQueryParameter("references")
    // 使用Safe Args的场景直接取Args里的references字符串即可
    val references = referencesStr?.split(",")?.mapNotNull { 
        it.toIntOrNull() 
    }?.toIntArray() ?: intArrayOf()
    
  • 如果你仅在应用内部使用DeepLink跳转,不需要兼容外部唤起场景,可以保留integer[]类型的参数定义,跳转时直接通过Bundle传入整型数组即可,无需拼接为字符串。

注意:转换过程中建议使用toIntOrNull()方法或增加异常捕获逻辑,避免非法数值导致应用崩溃。

内容的提问来源于stack exchange,提问作者Riajul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 01:36:03