How to pass RealmObject through the Intents Bundle? Is there an way to write RealmObject to parcel? I don't want to use Serializable for know reasons.
-
Than use parcable. Or Use singleton class to hold this object and set to null when it is no more for use. – Biraj Zalavadia Oct 20 '14 at 13:03
-
You cant: "RealmObjects are strongly tied to one Realm so they must be instantiated from the Realm using the realm.createObject() instance method.". Using singleton is also bad idea. – Dawid Hyży Nov 18 '14 at 12:24
3 Answers
You can't implement Parcelable in your Realm Model Classes, as mentioned in Realm Java Doc
Be aware that the getters and setters will be overridden by the generated proxy class used in the back by RealmObjects, so any custom logic you add to the getters & setters will not actually be executed.
But there is a work around that can fit for you, implementing Parceler Library you will be able to send objects across activities and fragments
Check out this closed issue on Realm Github https://github.com/johncarl81/parceler/issues/57
One of the answer show how to use Parceler with realm, it is necessary to set a custom params on @Parcel annotation.

- 246
- 2
- 7

- 3,475
- 3
- 19
- 23
The easiest solution is to use Parceler: https://realm.io/docs/java/latest/#parceler
For example:
// All classes that extend RealmObject will have a matching RealmProxy class created
// by the annotation processor. Parceler must be made aware of this class. Note that
// the class is not available until the project has been compiled at least once.
@Parcel(implementations = { PersonRealmProxy.class },
value = Parcel.Serialization.BEAN,
analyze = { Person.class })
public class Person extends RealmObject {
// ...
}

- 3,581
- 5
- 26
- 41
make your RealmObject implement Parcelable
, here's a typical implementation from Developers' doc:
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}

- 825
- 6
- 16
-
2I don't believe it is the right way to do it, because the realm docs say that "RealmObjects are strongly tied to one Realm so they must be instantiated from the Realm using the realm.createObject() instance method." http://realm.io/docs/java/0.72.0/#models – x-treme Oct 28 '14 at 06:22
-
1
-
@boban0987, how can we use Serializable? same way we use it for regular object? or that Realm object need something special? – Shahar May 25 '15 at 09:49