GVi*_*i82 5 android android-fragments android-navigation android-architecture-navigation android-navigation-graph
我正在使用导航组件,并且当用户点击通知时,我尝试触发到由片段表示的特定目的地的显式深度链接。
根据文档,可以像这样创建待定意图:
val bundle = bundleOf("id" to "1234")
val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.myDestination)
.setArguments(args)
.createPendingIntent()
Run Code Online (Sandbox Code Playgroud)
其中 nav_graph 定义如下:
<fragment
android:id="@+id/myDestination"
android:name="MyFragment">
<argument
android:name="id"
app:argType="string" />
<deepLink app:uri="myApp://myFragment?id={id}" /> // Removing this line it works fine
</fragment>
Run Code Online (Sandbox Code Playgroud)
然后,我将使用以下方法在通知中使用pendingIntent NotificationCompat.Builder:
.setContentIntent(pendingIntent)
Run Code Online (Sandbox Code Playgroud)
当我点击通知时,实际上打开了正确的目的地,但该值args.id将为“null”(不是null,而是一个具有“null”值的字符串。在我的片段中,我有
private val args by navArgs<MyFragmentArgs>()
...
override fun onCreate(saveInstanceState: Bundle?) {
args.id // The string value is "null".
}
Run Code Online (Sandbox Code Playgroud)
<deepLink>但是,如果我从片段中删除它,那么它就会起作用。问题是我需要隐式和显式的深层链接。有没有办法通过导航组件同时支持两者?
小智 1
val bundle = bundleOf("id" to "1234")
val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_graph)
.setDestination(R.id.myDestination)
.setArguments(args)
.createPendingIntent()
Run Code Online (Sandbox Code Playgroud)
你的变量是什么args,因为你bundle在上面声明了?
我已经检查过,所有代码都工作正常。
我的导航图:
<fragment
android:id="@+id/navigation_notifications"
android:name="com.olopa.thefirst.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications">
<deepLink app:uri="sampleapp://noti?id={id}" />
<argument
android:name="id"
app:argType="string"
app:nullable="true" />
Run Code Online (Sandbox Code Playgroud)
通过通知导航时:
val bundle = bundleOf("id" to "12")
val pendingIntent = NavDeepLinkBuilder(this)
.setGraph(R.navigation.mobile_navigation)
.setDestination(R.id.navigation_notifications)
.setArguments(bundle)
.createPendingIntent()
Run Code Online (Sandbox Code Playgroud)
或通过链接导航:
navController.navigate(Uri.parse("sampleapp://noti?id=1"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8667 次 |
| 最近记录: |