How can I make my custom objects Parcelable?

后端 未结 11 2558
旧巷少年郎
旧巷少年郎 2020-11-21 07:40

I\'m trying to make my objects Parcelable. However, I have custom objects and those objects have ArrayList attributes of other custom objects I have made.

11条回答
  •  日久生厌
    2020-11-21 08:11

    How? With annotations.

    You simply annotate a POJO with a special annotation and library does the rest.

    Warning!

    I'm not sure that Hrisey, Lombok, and other code generation libraries are compatible with Android's new build system. They may or may not play nicely with hot swapping code (i.e. jRebel, Instant Run).

    Pros:

    • Code generation libraries save you from the boilerplate source code.
    • Annotations make your class beautiful.

    Cons:

    • It works well for simple classes. Making a complex class parcelable may be tricky.
    • Lombok and AspectJ don't play well together. [details]
    • See my warnings.

    Hrisey

    Warning!

    Hrisey has a known issue with Java 8 and therefore cannot be used for Android development nowadays. See #1 Cannot find symbol errors (JDK 8).

    Hrisey is based on Lombok. Parcelable class using Hrisey:

    @hrisey.Parcelable
    public final class POJOClass implements android.os.Parcelable {
        /* Fields, accessors, default constructor */
    }
    

    Now you don't need to implement any methods of Parcelable interface. Hrisey will generate all required code during preprocessing phase.

    Hrisey in Gradle dependencies:

    provided "pl.mg6.hrisey:hrisey:${hrisey.version}"
    

    See here for supported types. The ArrayList is among them.

    Install a plugin - Hrisey xor Lombok* - for your IDE and start using its amazing features!


    * Don't enable Hrisey and Lombok plugins together or you'll get an error during IDE launch.


    Parceler

    Parcelable class using Parceler:

    @java.org.parceler.Parcel
    public class POJOClass {
        /* Fields, accessors, default constructor */
    }
    

    To use the generated code, you may reference the generated class directly, or via the Parcels utility class using

    public static  Parcelable wrap(T input);
    

    To dereference the @Parcel, just call the following method of Parcels class

    public static  T unwrap(Parcelable input);
    

    Parceler in Gradle dependencies:

    compile "org.parceler:parceler-api:${parceler.version}"
    provided "org.parceler:parceler:${parceler.version}"
    

    Look in README for supported attribute types.


    AutoParcel

    AutoParcel is an AutoValue extension that enables Parcelable values generation.

    Just add implements Parcelable to your @AutoValue annotated models:

    @AutoValue
    abstract class POJOClass implements Parcelable {
        /* Note that the class is abstract */
        /* Abstract fields, abstract accessors */
    
        static POJOClass create(/*abstract fields*/) {
            return new AutoValue_POJOClass(/*abstract fields*/);
        }
    }
    

    AutoParcel in Gradle build file:

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    
    repositories {
        /*...*/
        maven {url "https://clojars.org/repo/"}
    }
    
    dependencies {
        apt "frankiesardo:auto-parcel:${autoparcel.version}"
    }
    

    PaperParcel

    PaperParcel is an annotation processor that automatically generates type-safe Parcelable boilerplate code for Kotlin and Java. PaperParcel supports Kotlin Data Classes, Google's AutoValue via an AutoValue Extension, or just regular Java bean objects.

    Usage example from docs.
    Annotate your data class with @PaperParcel, implement PaperParcelable, and add a JVM static instance of PaperParcelable.Creator e.g.:

    @PaperParcel
    public final class Example extends PaperParcelable {
        public static final PaperParcelable.Creator CREATOR = new PaperParcelable.Creator<>(Example.class);
    
        private final int test;
    
        public Example(int test) {
            this.test = test;
        }
    
        public int getTest() {
            return test;
        }
    }
    

    For Kotlin users, see Kotlin Usage; For AutoValue users, see AutoValue Usage.


    ParcelableGenerator

    ParcelableGenerator (README is written in Chinese and I don't understand it. Contributions to this answer from english-chinese speaking developers are welcome)

    Usage example from README.

    import com.baoyz.pg.Parcelable;
    
    @Parcelable
    public class User {
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
    }
    

    The android-apt plugin assists in working with annotation processors in combination with Android Studio.

提交回复
热议问题