You can use top functions to serialize arguments
but I had a problem serializing the List of object
You can use this way to put and get serialized argument data
Because List is not a Serializable Class, you need to convert it to Array List that supports serializable
These functions are used for serializable
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? =
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ->
getSerializable(key, T::class.java)
else -> @Suppress("DEPRECATION") getSerializable(key) as? T
}
inline fun <reified T : Serializable> Intent.serializable(key: String): T? =
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ->
getSerializableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T
}
and this way handle puts serialized List of objects
class YourFragment: Fragment {
private latinit var list: List<YourObject>
fun newInstance(
listOfYourObject: List<YourObject>
): YourFragment {
val args = Bundle()
val yourList= ArrayList<YourObject>()
yourList.addAll(listOfYourObject)
args.putSerializable(LIST_KEY, yourList)
val fragment = YourFragment()
fragment.arguments = args
return fragment
}
}
and now can get serialize of your list object in this way
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requireArguments().serializable<ArrayList<YourObject>>(LIST_KEY)?.let{
list = it.toList()
}
}