How to use Parcelable on non-primitive types?

2019-07-30 01:07发布

问题:

I have a class TableMetaAttrs:

public class TableMetaAttrs implements Parcelable
{
    private Integer id;
    private String name;
    private String value;
    private Tables table;
...

How do i implement Parcelable on table field? Class Tables also implements Parcelable.

回答1:

You have to make the Tables class implements Parcelable as well.

That way whenever TableMetaAttrs is parcelling or re-building itself, it can call putParcelable and getParcelable for the table field.

edit:

I'm assuming Tables is a class you created, so it's signature must be

public class Tables implements Parcelable{
    // and somewhere in this code tables have to parcel all it's primitives
}

then whenever TableMetaAttrs is parcelling it's values it calls:

 parcel.putParcelable(table);

and rebuilding it:

  table = parcel.getParcelable();