代码之家  ›  专栏  ›  技术社区  ›  bakua

带有查询参数的Jetpack导航deeplink

  •  5
  • bakua  · 技术社区  · 6 年前

    在新的Jetpack导航库中,使用查询参数处理deeplink似乎是不可能的。如果你把下面的导航.xml: <deepLink app:uri="scheme://host/path?query1={query_value}" /> 那么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))
    }
    

    为了让考试通过,我要做的就是逃避考试?具体如下:

    @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))
    }
    

    我是不是缺少了一些真正基本的功能来将查询值传递到片段中,或者这是目前不支持的特性?

    2 回复  |  直到 6 年前
        1
  •  5
  •   Hamed Jaliliani    5 年前

    您需要将DeepLink导航添加到AndroidManifest.xml(处理片段的特殊活动)因此,当deeplink单击时,您的应用程序可以接收deeplink并将其传递给导航,fragment&可以将其作为参数读取:

    我把科特林密码放在这里:

    在导航文件中,处理deeplink和arguements的片段必须如下所示:

    <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>
    

    阅读frasgment/Activity中的deeplink参数:

    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
            android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar" >
    
        <nav-graph android:value="@navigation/main_navigation"/>
    
    </activity>
    
        2
  •  0
  •   Tom Koptel    6 年前
    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.