So, I have a really weird issue, I pass an object from Fragment A to Fragment B , I modify this object in a new instance in Fragment B, but after I change a value on this object it also changes that value when I pop Framgment B and that object keeps modified now also for Fragment A
Fragment A
...
override fun onItemClick(v: View?, position: Int) {
searchView.clearFocus()
val bundle = Bundle()
bundle.putSerializable("shop", landingAdapter.getItem(position))
findNavController().navigate(R.id.action_navigation_landing_to_shopFragment, bundle)
}
...
Now, from Fragment B I get this object
Fragment B
private lateinit var shop: Shop
private lateinit var shopAdapter:ShopAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shopAdapter = ShopAdapter(sharedViewModel, requireContext(),this)
arguments?.let {
shop = it.getSerializable(ARG_SHOP) as Shop
if (shop.products.isNotEmpty()) {
shopAdapter.setItems(shop.products)
}
}
}
Now, after I get this Shop object from Fragment A, I modify it in Fragment B only with
onViewCreated(){
shop.quantity = 1
}
but when I go back to Fragment A, now that Shop object quantity value is 1 , but it should be nothing since I have only changed the object at Fragment B not Fragment A , and in Fragment B is a new instance of that object
I'm really confused
EDIT
What I have tried so far to send a fresh instance each time I go from Fragment A to Fragment b
val bundle = Bundle()
bundle.putSerializable("shop", landingAdapter.getItem(position).copy())
findNavController().navigate(R.id.action_navigation_landing_to_shopFragment, bundle)
val bundle = Bundle()
val shop = landingAdapter.getItem(position)
bundle.putSerializable("shop", shop) findNavController().navigate(R.id.action_navigation_landing_to_shopFragment, bundle)
val bundle = Bundle()
val shop = Shop(landingAdapter.getItem(position).name,landingAdapter.getItem(position).quantity)
bundle.putSerializable("shop", shop)
findNavController().navigate(R.id.action_navigation_landing_to_shopFragment, bundle)
None of them sends a fresh instance of shop to Fragment B, so whenever I change quantity at fragment B, fragment A gets the same quantity value which should not mutate