Android 导航组件使用 safeArgs 和深层链接传递参数

Ma2*_*340 7 android kotlin android-jetpack android-jetpack-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">
        <deepLink app:uri="App://FragmentB" />
    </fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)

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

val uri = Uri.parse("App://FragmentB")
findNavController().navigate(uri)
Run Code Online (Sandbox Code Playgroud)

我知道如何使用safeArgs没有深层链接。如何将数据传递到具有深层链接的其他功能?

Fra*_*del 14

您可以执行以下操作

首先添加参数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>
Run Code Online (Sandbox Code Playgroud)

然后你可以这样称呼它

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())
Run Code Online (Sandbox Code Playgroud)

编辑

没有通知

首先,在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>
Run Code Online (Sandbox Code Playgroud)

然后你可以这样称呼它

val uri = Uri.parse("App://FragmentB/yourarg")
findNavController().navigate(uri)
Run Code Online (Sandbox Code Playgroud)

然后你可以用这段代码获取参数

val argument = arguments?.getString("yourarg")

// Or with Safe Args

val safeArgs: FragmentBArgs by navArgs()
val yourarg = safeArgs.yourarg
Run Code Online (Sandbox Code Playgroud)

参考文献: https://codelabs.developers.google.com/codelabs/android-navigation#9

  • 这仅处理字符串数据类型,但在许多应用程序中,我们需要传递带有深层链接的自定义对象。有什么我们可以做的吗? (4认同)