Procedures for passing RealmObject to Fragment and how to use Parceler

Introduction

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.

Thing you want to do

public static Fragment newInstance(SampleRealmObject realmObject) {
    Bundle args = new Bundle();
    args.putParcelable("key", realmObject);
    ...
}

way

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)

Conclusion

  1. Added description to build.gradle of application
compile 'org.parceler:parceler-api:1.1.9'
annotationProcessor 'org.parceler:parceler:1.1.9'

Click here for details https://github.com/johncarl81/parceler

  1. Add @Parcel to RealmObject
@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

  1. Add Parcels.wrap when puttingParcelable
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

Procedures for passing RealmObject to Fragment and how to use Parceler
How to use StringBurrer and Arrays.toString.
How to use equality and equality (how to use equals)
How to use Font Awesome icon for ul and li
How to use \ t Escape sequences different for mac and Windows-java
How to use binding.pry for view files
How to use OrientJS and OrientDB together
How to set up and use kapt
[Ruby] How to use slice for beginners
How to use substring and substr methods
How to use @Builder and @NoArgsConstructor together
(For beginners) [Rails] Time saving tech! How to install and use slim
How to use Segmented Control and points to note
How to use scope and pass processing (Jakarta)
[Rails] How to use Gem'rails-i18n' for Japanese support
How to use nginx-ingress-controller with Docker for Mac
[For super beginners] How to use autofocus: true
[Java] How to use Calendar class and Date class
Thinking about how to use Ionic-Native, Hybrid and HTML5 for mobile app production
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use an array for a TreeMap key
How to use Truth (assertion library for Java / Android)
[For those who create portfolios] How to use font-awesome-rails
How to use and apply Java's JFrame / Canvas class
How to use GitHub for super beginners (team development)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use SAS tokens for Azure Event hubs (Java)
How to use OpenCV 4 on Android and view camera live view
[Must-see for apprentice java engineer] How to use Stream API
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView