![](/img/trans.png)
[英]How to pass a List as an argument to a Fragment using Navigation Component safeargs
[英]How to pass object of type Parcelable to a Fragment using Navigation type safeargs plugin
我正在重写我的简单 UI 应用程序以使用导航架构组件,我需要传递一个实现 Parcelable 的 Pojo,还没有看到任何关于如何做到这一点的文档。
任何帮助,将不胜感激。
由于safe-args-gradle-plugin:1.0.0-alpha03
可以使用Parcelable
通过使用其完全限定类名称的对象:
<argument
android:name="item"
app:argType="com.example.app.model.Item"/>
现在支持 Parcelable 参数,使用 app:type 的完全限定类名。 支持的唯一默认值是“@null”( https://issuetracker.google.com/issues/79563966 )
来源: https : //developer.android.com/jetpack/docs/release-notes
为了支持可空性,必须使用android:defaultValue="@null"
和app:nullable="true"
。
我知道答案已经存在,但这可能会对某人有所帮助。 Code snippet
在 build.gradle 添加这个依赖
ext{
...
navigation_version = '1.0.0-alpha11'
}
dependencies {
...
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}
在 app/build.gradle 中
apply plugin: 'androidx.navigation.safeargs'
...
在导航图中
<fragment
android:id="@+id/source_fragment_id"
android:name="app.test.SourceFragment"
android:label="@string/source_fragment_label"
tools:layout="@layout/source_fragment_layout">
<action
android:id="@+id/action_source_fragment_to_destination_fragment"
app:destination="@id/destination_fragment_id"
...
/>
</fragment>
<fragment
android:id="@+id/destination_fragment_id"
android:name="app.test.DestinationFragment"
android:label="@string/destination_fragment_label"
tools:layout="@layout/destination_fragment_layout">
<argument
android:name="variableName"
app:argType="app.test.data.model.CustomModel" />
...
</fragment>
注意:CustomModel 应该是 Parcelable 或 Serializable。
从 SourceFragment 导航到此 DestinationFragment 时
val direction = SourceFragmentDirections.ActionSourceFragmentToDestinationFragment(customModel)
findNavController().navigate(direction)
现在从 DestinationFragment 中的 bundle 中检索值
...
import app.test.DestinationFragmentArgs.fromBundle
class DestinationFragment : Fragment() {
val variableName by lazy {
fromBundle(arguments!!).variableName
}
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e(DESTINATION_FRAGMENT_TAG,"onCreateView")
//Can use CustomModel variable here i.e. variableName
}
}
现在,除了integer 、 string 、 inferred和reference之外,您不能将安全参数用于其他类型,还有一个问题需要其他类型。
您现在可以做的是在使用navigate()方法导航到目的地时通常传递一个包:
var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
您可以使用通常的getArguments
(或 kotlin 中的参数)来检索:
val tv = view.findViewById(R.id.textViewAmount)
tv.text = arguments.getString("amount")
您可以使用boolean
、 reference
、 integer
、 long
、 string
、 enum
、 parcelable
甚至可serializable
。 但是忘记最后一个 ;-)
最好使用最新的插件版本safe-args-gradle-plugin:1.0.0-alpha08
并指定完全限定的类名:
<fragment
...>
<argument
android:name="data"
app:argType="com.example.ParcelableData" />
</fragment>
从你的
package com.example
data class ParcelableData(val content: String) : Parcelable { ... }
您可以发送所有argType
的数组:
<argument
android:name="data"
app:argType="string[]" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.