我有一个如下功能的导航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 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
上面的答案显示了如何使用字符串数据类型。但是,如果您想传递自定义对象,安全参数也可以很好地工作;
<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>
将自定义属性传递给deeplink;
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