【问题标题】:Android navigation component passing argument with safeArgs and deep linkAndroid 导航组件通过 safeArgs 和深度链接传递参数
【发布时间】:2020-12-13 21:12:13
【问题描述】:

我有如下功能的导航 xml -

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation_featureB.xml"
    app:startDestination="@id/FragmentB">

    <fragment
        android:id="@+id/FragmentB"
        android:name="FragmentB"
        android:label="FragmentB">
        <deepLink app:uri="App://FragmentB" />
    </fragment>
</navigation>

在我的 FeatureA 片段中,我执行以下操作 -

val uri = Uri.parse("App://FragmentB")
findNavController().navigate(uri)

我知道如何在没有深度链接的情况下使用safeArgs。 如何通过深层链接将数据传递给其他功能?

【问题讨论】:

    标签: android kotlin android-jetpack android-jetpack-navigation


    【解决方案1】:

    您可以执行以下操作

    首先在navigation.xml中添加参数

    <?xml version="1.0" encoding="utf-8"?>
    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation_featureB.xml"
        app:startDestination="@id/FragmentB">
    
        <fragment
            android:id="@+id/FragmentB"
            android:name="FragmentB"
            android:label="FragmentB">
            <argument
                android:name="yourarg"
                android:defaultValue="Argument Default Value"/>
            <deepLink app:uri="App://FragmentB/{yourarg}" />
        </fragment>
    </navigation>
    

    那你就可以这样称呼了

    val args = Bundle()
    args.putString("yourarg", "Argument Value")
    
    val deeplink = findNavController().createDeepLink()
                   .setDestination(R.id.FragmentB)
                   .setArguments(args)
                   .createPendingIntent()
    val builder = NotificationCompat.Builder(
                    context, "deeplink")
                    .setContentTitle("Deep link with data")
                    .setContentText("Deep link with data to Android")
                    .setSmallIcon(R.drawable.ic_android)
                    .setContentIntent(deeplink)
                    .setAutoCancel(true)
    val notificationManager =
                    context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(0, builder.build())
    

    已编辑

    无需通知

    首先,在navigation.xml中添加参数

    <?xml version="1.0" encoding="utf-8"?>
    <navigation xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation_featureB.xml"
        app:startDestination="@id/FragmentB">
    
        <fragment
            android:id="@+id/FragmentB"
            android:name="FragmentB"
            android:label="FragmentB">
            <argument
                android:name="yourarg"
                android:defaultValue="Argument Default Value"/>
            <deepLink app:uri="App://FragmentB/{yourarg}" />
        </fragment>
    </navigation>
    

    那你就可以这样称呼了

    val uri = Uri.parse("App://FragmentB/yourarg")
    findNavController().navigate(uri)
    

    那么你就可以用这段代码得到参数了

    val argument = arguments?.getString("yourarg")
    
    // Or with Safe Args
    
    val safeArgs: FragmentBArgs by navArgs()
    val yourarg = safeArgs.yourarg
    

    参考资料: https://codelabs.developers.google.com/codelabs/android-navigation#9

    【讨论】:

    • 没有别的办法了吗?我只想在同一个应用的其他功能中打开一个片段,我需要通知吗?
    • 请检查我更新的答案,我提供了不使用通知的方式
    • 这非常感谢:) 我已经接受了答案。但是是否有可能将 safeArgs 与深层链接一起使用?
    • 当然,很高兴它有帮助 :) 是的,可以使用 safeArgs,我再次更新了答案 :)
    • 当然,没问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2020-03-02
    • 2016-03-31
    • 2019-05-12
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多