When I was using Realm and wanted to pass it to Fragment, I implemented Serializable and putSerializable to Bundle, and it was forcibly terminated with onPause. I found out how to implement it by investigating whether it can be passed in the first place, so make a note of it. If you just want to solve it, see the conclusion.
public static Fragment newInstance(SampleRealmObject realmObject) {
Bundle args = new Bundle();
args.putParcelable("key", realmObject);
...
}
First of all, RealmObject was a model class that did not do anything particularly complicated, so I thought that if I implemented Serializable, I could putSerializable, so I implemented it. Then, I passed it well and the app worked, but ... When you turn off the screen or return to the home screen
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = io.realm.SampleRealmObjectRealmProxy)
Caused by: java.io.NotSerializableException: io.realm.SampleRealmObjectRealmProxy$SapmleRealmObjectColumnInfo
I was told. I wondered if Serialize is impossible because Proxy is involved, and I wondered if Parcelable could go. Discover this article https://realm.io/jp/docs/java/latest/#parceler Even though the link destination is listed, I added the description that was honestly described in Realm without reading it
compile "org.parceler:parceler-api:1.0.3"
apt "org.parceler:parceler:1.0.3"
I found this article when I was searching because it does not work well http://qiita.com/kazhida/items/affe4488078a2e625d33#%E3%82%A2%E3%83%8E%E3%83%86%E3%83%BC % E3% 82% B7% E3% 83% A7% E3% 83% B3% E3% 83% A9% E3% 82% A4% E3% 83% 96% E3% 83% A9% E3% 83% AA When I added the description and built it, it passed, so one case settled down. I thought, a warning was displayed.
Warning:Using incompatible plugins for the annotation processing: android-apt. This may result in an unexpected behavior.
android-apt is not good and may behave unexpectedly. When. Then, if you google, you will find the following article. https://stackoverflow.com/questions/42632662/android-studio-warning-using-incompatible-plugins-for-the-annotation-processing If you read the best answer, you will find an example of Butter Knife. It says "apply plugin: delete'com.neenbedankt.android-apt'" When I erase it and execute it, I get an error that there is no annotationProcessor. By comparison, my code didn't have an annotationProcessor, so maybe this is also in Parceler? I went to the library page and searched for it, and it was written in gradle. If you write it, it will be completed without warning. The story that Realm's description should not have been swallowed. (Maybe there is some good way)
compile 'org.parceler:parceler-api:1.1.9'
annotationProcessor 'org.parceler:parceler:1.1.9'
Click here for details https://github.com/johncarl81/parceler
@Parcel(implementations = SampleRealmObjectRealmProxy.class,
value = Parcel.Serialization.BEAN,
analyze = SampleRealmObject.class)
public class SampleRealmObject extends RealmObject {
...
}
Click here for details https://realm.io/jp/docs/java/latest/#parceler
public static Fragment newInstance(SampleRealmObject realmObject) {
Bundle args = new Bundle();
args.putParcelable("key", Parcels.wrap(realmObject));
...
}
//Unwrap when restoring
SampleRealmObject realmObject = Parcels.upwrap(getArguments().getParcelable("key"));
that's all!
Recommended Posts