0
package com.iconasystems.android.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.parse.ParseClassName;
import com.parse.ParseObject;

import java.util.UUID;

/**
 * Created by Alex on 2/15/2016.
 */
@ParseClassName("Lock")

public class Lock extends ParseObject implements Parcelable {
    private String simNumber;
    private User owner;
    private String authToken;
    private String state;
    private String id;
    private String doorName;
    public Lock() {
    }

    public Lock(Parcel parcel){
        this.doorName = parcel.readString();
        this.authToken = parcel.readString();
        this.simNumber = parcel.readString();
        this.state = parcel.readString();
        this.id = parcel.readString();

    }

    // Some getters and setters

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(doorName);
        parcel.writeString(authToken);
        parcel.writeString(simNumber);
        parcel.writeString(state);
        parcel.writeString(id);

    }

    final Creator<Lock> CREATOR = new Creator<Lock>() {
        @Override
        public Lock createFromParcel(Parcel parcel) {
            return new Lock(parcel);
        }

        @Override
        public Lock[] newArray(int i) {
            return new Lock[i];
        }
    };


}

A Lock is owned by a User (has its own model) so its has an attribute owner of type User. My major problem is writing the user object into the parcel. Is there a way of writing custom types into a parcel.

christoandrew
  • 437
  • 4
  • 17

1 Answers1

1

Is there a way of writing custom types into a parcel.

Yes. Your User class has to implement Parcelable and then in your writeToParcel method you can use parcel.writeParcelable(user, i)

Parcel can also work with classes implementing Serializable using parcel.writeSerializable. This requires significantly less work on the your part but because Serializable uses reflection, it'll come with a performance penalty.

asadmshah
  • 1,338
  • 9
  • 7