寻找一种更优雅的方式来使用 Jetpack 传输数据
现在我传递数据如下:
override fun onListItemClick(itemIndex: Int, itemCode: String) {
val bundle = Bundle()
bundle.putString(KEY_TARGET_GUID, (adapter.getItem(itemIndex) as Target).guid)
findNavController().navigate(R.id.target_edit, bundle)
}
并像这样在另一个片段中获取它:
private val targetGuid: String
get() = arguments?.getString(KEY_TARGET_GUID, "") ?: ""
我看过谷歌的人在codelab中做的,但在他的例子中他们创建了类FlowStepFragmentArgs
,而且它是大量的
data class FlowStepFragmentArgs(val flowStepNumber: Int = 2) : NavArgs {
fun toBundle(): Bundle {
val result = Bundle()
result.putInt("flowStepNumber", this.flowStepNumber)
return result
}
companion object {
@JvmStatic
fun fromBundle(bundle: Bundle): FlowStepFragmentArgs {
bundle.setClassLoader(FlowStepFragmentArgs::class.java.classLoader)
val __flowStepNumber : Int
if (bundle.containsKey("flowStepNumber")) {
__flowStepNumber = bundle.getInt("flowStepNumber")
} else {
__flowStepNumber = 2
}
return FlowStepFragmentArgs(__flowStepNumber)
}
}
}
问:在我的情况下,我怎样才能漂亮地传输数据