Looking for a more elegant way to transmit data with Jetpack
Now I am passing the data as follows:
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)
}
And get it in another fragment smth like this:
private val targetGuid: String
get() = arguments?.getString(KEY_TARGET_GUID, "") ?: ""
I watched the guys at google do it in codelab, but in his example they created class FlowStepFragmentArgs
, and it is voluminous
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)
}
}
}
Q: How can I transfer data beautifully in my case