Android导航组件通过safeArgs和深度链接传递参数
我有一个如下特性的导航xml-Android导航组件通过safeArgs和深度链接传递参数,android,kotlin,android-jetpack,android-jetpack-navigation,Android,Kotlin,Android Jetpack,Android Jetpack 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=&quo
<?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>
我知道如何在没有深度链接的情况下使用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>
<?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>
已编辑
未经通知
首先,在navigation.xml中添加参数
然后,您可以使用此代码获取参数
val argument = arguments?.getString("yourarg")
// Or with Safe Args
val safeArgs: FragmentBArgs by navArgs()
val yourarg = safeArgs.yourarg
参考资料:
您可以执行以下操作
首先在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>
<?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>
已编辑
未经通知
首先,在navigation.xml中添加参数
然后,您可以使用此代码获取参数
val argument = arguments?.getString("yourarg")
// Or with Safe Args
val safeArgs: FragmentBArgs by navArgs()
val yourarg = safeArgs.yourarg
参考资料:
没有别的办法了吗?我只想在同一个应用程序的其他功能中打开一个片段,我需要通知吗?请检查我的更新答案,我已经提供了不使用通知的方式这非常有帮助:)我已经接受了答案。但是有可能使用带有深度链接的safeArgs吗?当然,很高兴它有帮助:)是的,有可能使用safeArgs,我再次更新了答案:)太棒了:)谢谢你,没有其他方法了吗?我只想在同一个应用程序的其他功能中打开一个片段,我需要通知吗?请检查我的更新答案,我已经提供了不使用通知的方式这非常有帮助:)我已经接受了答案。但是有可能使用带有深度链接的safeArgs吗?当然,很高兴它有帮助:)是的,有可能使用safeArgs,我再次更新了答案:)太棒了:)非常感谢