我正在尝试使用新的Navigation Component API v1.0.0-alpha05实现深层链接功能,但遇到了问题。
使用Android Studio 3.3 Canary 7
我的navigation_graph.xml的一部分
<fragment
android:id="@+id/noteDetailFragment"
android:name="com.myapp.notes.notedetail.NoteDetailFragment"
android:label="@string/label_note_detail"
tools:layout="@layout/note_detail_fragment">
<argument
android:name="noteId"
android:defaultValue="0"
app:argType="integer" />
<action
android:id="@+id/action_noteDetail_to_editNote"
app:destination="@id/editNoteFragment" />
<deepLink
android:id="@+id/noteDetailDeepLink"
app:uri="notesapp://notes/{noteId}" />
</fragment>
AndroidManifest.xml包含:
<activity android:name=".presentation.MainActivity">
<nav-graph android:value="@navigation/navigation_graph" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我正在测试我与adb shell am start -a android.intent.action.VIEW -d "notesapp://notes/2" com.myapp.notes
的深层链接
在noteId
或NoteDetailFragmentArgs.fromBundle(arguments).noteId
中不存在arguments?.getInt("noteId", 0)
(在两种情况下都返回0
的默认值)
打印出Bundle表明它在那里:
[{android-support-nav:controller:deepLinkIntent=Intent { act=android.intent.action.VIEW dat=notesapp://notes/2 flg=0x1000c000 pkg=com.mynotes.notes cmp=com.mynotes.notes.presentation.MainActivity }, noteId=2}]
如果深层链接uri是http://www.mynotes.com/notes/2
,则会出现同样的问题
深度链接时如何访问noteId
?谢谢!
根据this issue,从深层链接解析的参数仅作为字符串添加到arguments
Bundle。
因此,您应该通过arguments?.getString("noteId")?.toInt()
检索noteId,直到该问题得到解决。
该问题已在android.arch.navigation 1.0.0-alpha09中修复。
Bug修复
参数现在可以从深层链接中正确解析为正确的argType,而不是始终作为字符串b/110273284
Arch Components发行说明:https://developer.android.com/jetpack/docs/release-notes
Android导航库将始终将占位符参数解析为字符串,除非在Nav Graph中将类型显式声明为参数。
对于{noteId}
的占位符,通过将参数声明为整数来接收传递的参数作为整数。
<argument
android:name="id"
app:argType="integer"
android:defaultValue="0" />