0

I'm using Parceler library for serialization in my project.

I have a RealmObject class like this:

@Parcel(implementations = {ARealmProxy.class}, value = Parcel.Serialization.BEAN, analyze = {A.class})
class A extends RealmObject {

    public int id;
    public int title;
}

I serialize an A object and put it into Intent like this:

Intent intent = new Intent(context, Main);
Bundle bundle = new Bundle();
A a = new A();
a.id = 10;
a.title = "title";
bundle.putParcelable("mykey", Parcels.wrap(a))
intent.putExtras(bundle);
context.startActivity(intent);

And I deserialize it like this:

Bundle bundle = getIntent().getExtras();
A a = Parcels.unwrap(bundle.getParcelable("mykey"));
// a's properties are null

And a's properties are null. How I can fix this?

Hojjat
  • 815
  • 1
  • 10
  • 25

1 Answers1

0

You would need to use getters/setters.

@Parcel(implementations = {ARealmProxy.class}, 
        value = Parcel.Serialization.BEAN, 
        analyze = {A.class})
class A extends RealmObject {
    @PrimaryKey 
    private int id;

    private int title;

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public int getTitle() { return title; }
    public void setTitle(int title) { this.title = title; }
}

Although technically you shouldn't be creating Parcelable objects from the RealmObject. You should be sending the primary key through the intent bundle, and re-query the object in the other activity.

Intent intent = new Intent(context, Main.class);
Bundle bundle = new Bundle();
bundle.putLong("id", 10);

And

Bundle bundle = getIntent().getExtras();
A a = realm.where(A.class).equalTo(AFields.ID, bundle.getLong("id")).findFirst();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428