0

I have a fragment and I create a new Instance of it, in my Activity, Also I'm passing a Parcelable data class to the fragment like this:

fun newInstance(
            use: User,
            date: Date
        ): CalendarDialogFragment {
            val fragment = MyFragment()
            val bundle = Bundle()
            bundle.putParcelable("user", user)
            bundle.putSerializable("date", date)
            fragment.arguments = bundle
            return fragment
        }

And in the fragment I want to use it like this:

var user =  arguments?.getParcelable("user")
var date =  arguments?.getSerializable("date")

This is a normal way of passing data to fragments. because of shallow copy, When I change this user object and update its field in the fragment, the bundle object will be updated too. But for some objects like the Date (java.util) type, it's not happening. For example, if I pass a Date to the fragment and do the same, the bundle object of date will not update like the user object, I think I'm missing something here!?

Ehsan
  • 2,676
  • 6
  • 29
  • 56

1 Answers1

0

Here you would need to make a deep copy of the user, i.e., a copy that doesn't copy references, but actually copies the object the references are pointing at. I think you need to understand deep copy and shallow copy. You can read it here What is the difference between a deep copy and a shallow copy?

  • Thanks for the info, I know the difference between copies, But when I pass Date and User, I'm using a shallow copy for both of them so I expect that the date and user object in the bundle will update by the reference but it's not happened for Date object and I don't know why – Ehsan Apr 21 '22 at 07:27