0

I have two object like these that I want to send through Intent implementing Parcelable interface:

class Foo implements Parcelable
{
    private Bar bar;

    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeParcelable(bar, flags);
    }
}

class Bar implements Parcelable
{
    private Foo foo;

    public void writeToParcel(Parcel dest, int flags)
    {
        dest.writeParcelable(foo, flags);
    }
}

How can I implement correctly the Parcelable interface?

1 Answers1

0

I would do it this way:

In writeToParcel of Foo class write all fields of Foo instance without Bar field and all fields of Bar instance without Foo field.

Then in Foo(Parcel in) read all fields of Foo and use fields of Bar to create the Bar instance and link both objects together.

RadekJ
  • 2,835
  • 1
  • 19
  • 25
  • ok, thank you, I've understand. and if I've got another object which contains a `DateTime` field, should I use `writeValue` or `writeTypedObject` to write this field? –  Mar 08 '17 at 18:15
  • 1
    You can use `writeValue` if `DateTime` is `Serializable` or `Parcelable` and `writeTypedObject` only if `DateTime` is `Parcelable`. – RadekJ Mar 08 '17 at 18:22