是否可通过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
相关产品推荐
相关产品推荐

