Android 导航组件使用 safeArgs 和 deep 传递参数 link
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
没有深度 link。
如何将数据传递给具有深度 link 的其他功能?
您可以执行以下操作
首先在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
我有一个导航 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
没有深度 link。
如何将数据传递给具有深度 link 的其他功能?
您可以执行以下操作
首先在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