繁体   English   中英

Android 导航组件通过 safeArgs 和深度链接传递参数

[英]Android navigation component passing argument with safeArgs and deep link

我有一个导航 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 如何通过深度链接将数据传递给其他功能?

您可以执行以下操作

首先在navigation.xml中添加参数。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/yourarghere")
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

以上答案显示了如何使用字符串数据类型。 但是如果你想传递自定义对象,safe-args 也很有效;

<fragment
    android:id="@+id/verification_fragment"
    android:name="com.appname.ui.verification.VerificationFragment"
    android:label="VerificationFragment">
    <argument
        android:name="verificationType"
        android:defaultValue="PHONE"
        app:argType="com.appname.core.model.VerificationType" />
    <deepLink app:uri="android-app://com.appname/verification_fragment?verificationType={verificationType}" />
</fragment>

将自定义属性传递给深度链接;

            val request = NavDeepLinkRequest.Builder
            .fromUri("android-app://appname/verification_fragment?verificationType=${VerificationType.MAIL}".toUri())
            .build()
        findNavController().navigate(request)

从目标片段中读取属性;

private val args: VerificationFragmentArgs by navArgs()

args.verificationType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM