【问题标题】:Jetpack Navigation deeplink with query parameters带有查询参数的 Jetpack Navigation 深层链接
【发布时间】:2018-06-16 10:48:50
【问题描述】:

似乎无法在新的 Jetpack 导航库中处理带有查询参数的深层链接。如果将以下内容放入 navigation.xml: <deepLink app:uri="scheme://host/path?query1={query_value}" /> 那么深度链接不会打开片段。

经过一番挖掘,我发现罪魁祸首可能在 NavDeepLink 中,它将 url 从 xml 转换为 Pattern 正则表达式。看起来问题是一个没有被排除的问号。

我写了一个失败的测试:

@Test
fun test() {
    val navDeepLink = NavDeepLink("scheme://host/path?query1={query_value}")
    val deepLink = Uri.parse("scheme://host/path?query1=foo_bar")
    assertEquals(true, navDeepLink.matches(deepLink))
}

要使测试通过,我所要做的就是逃避 ?如下:

@Test
fun test() {
    val navDeepLink = NavDeepLink("scheme://host/path\\?query1={query_value}")
    val deepLink = Uri.parse("scheme://host/path?query1=foo_bar")
    assertEquals(true, navDeepLink.matches(deepLink))
}

我是否遗漏了一些非常基本的东西来将查询值传递给我的 Fragment,还是目前不支持此功能?

【问题讨论】:

    标签: android regex url query-parameters android-architecture-navigation


    【解决方案1】:

    您需要将 DeepLink Navigation 添加到 AndroidManifest.xml(处理片段的特殊 Activity),因此当点击 deeplink 时,您的应用可以接收 DeepLink 并将其传递给该导航和片段并可以将其作为参数读取:

    我会把 Kotlin 代码放在这里:

    在您的导航文件中,您将处理带有争论的深层链接的片段必须是这样的:

    <fragment
            android:id="@+id/menu"
            android:name="ir.hamplus.fragments.MainFragment"
            android:label="MainFragment">
    
        <action android:id="@+id/action_menu_to_frg_messenger_main" 
         app:destination="@id/frg_messenger_main"/>
    
        <deepLink app:uri="http://hamplus.ir/request/?key={key}&amp;id={id}" />
    
        <argument android:name="key" app:argType="string"/>
        <argument android:name="id" app:argType="string"/>
    
    </fragment>
    

    读取片段 /Activity 中的深层链接参数:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
      //Or in activity read the intent?.data
           arguments?.let {
                Log.i("TAG", "Argument=$arguments")
    
                var key = it.getString("key")
                Log.i("TAG", "key=$key")
    
                var id = it.getString("id")
                Log.i("TAG", "id=$id")
    
            }
    }
    

    同时在相关Activity中的AndroidManifest.xml上添加导航图:

     <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" >
    
        <nav-graph android:value="@navigation/main_navigation"/>
    
    </activity>
    

    【讨论】:

    • 如果activity不是main,怎么启动呢?
    【解决方案2】:
    package androidx.navigation
    
    import android.net.Uri
    import androidx.test.runner.AndroidJUnit4
    import org.junit.Assert.assertTrue
    import org.junit.Test
    import org.junit.runner.RunWith
    
    @RunWith(AndroidJUnit4::class)
    class NavTest {
        @Test
        fun test() {
            val navDeepLink = NavDeepLink("scheme://host/path\\?query1={query_value1}&query2={query_value2}")
            val deepLink = Uri.parse("scheme://host/path?query1=foo_bar&query2=baz")
    
            val bundle = navDeepLink.getMatchingArguments(deepLink)!!
            assertTrue(bundle.get("query_value1") == "foo_bar")
            assertTrue(bundle.get("query_value2") == "baz")
        }
    }
    

    最后看起来 NavDeepLink 将非转义视为“?” match-zero-or-one quantifier。你需要逃避它。换句话说,我们泄露了未记录的实现细节。

    它可能与这种情况无关,但是用“\”转义“&”有一些类似的问题when using add command.

    这个问题也被触及in the following channel.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多