Code example for Parcel

Methods: dataPosition, readBooleanArray, readByte, readString, setDataPosition, writeBooleanArray, writeByte, writeString

0
     * @param source LoginDetails item to copy. 
     */ 
    public void copy(LoginDetails source) {
        android.os.Parcel _data = android.os.Parcel.obtain();
        source.writeToParcel(_data, 0);
        _data.setDataPosition(0);
        readFromParcel(_data);
    } 
 
    /** 
     * Default constructor. 
     */ 
    public LoginDetails() { 
    } 
 
    /** 
     * Enumeration of data items in LoginDetails Parcel. 
     */ 
    private enum MemberData { 
        USERNAME,
        PASSWORD,
        MOBILE_NO,
        SUBSCRIBER_ID;
    } 
 
    /** 
     * Read LoginDetails item from supplied Parcel. 
     *  
     * @param in Parcel containing LoginDetails. 
     */ 
    private void readFromParcel(Parcel in) {
        mUsername = null;
        mPassword = null;
        mMobileNo = null;
 
        boolean[] validDataList = new boolean[MemberData.values().length];
        in.readBooleanArray(validDataList);
 
        if (validDataList[MemberData.USERNAME.ordinal()]) {
            mUsername = in.readString();
        } 
 
        if (validDataList[MemberData.PASSWORD.ordinal()]) {
            mPassword = in.readString();
        } 
 
        if (validDataList[MemberData.MOBILE_NO.ordinal()]) {
            mMobileNo = in.readString();
        } 
 
        if (validDataList[MemberData.SUBSCRIBER_ID.ordinal()]) {
            mSubscriberId = in.readString();
        } 
 
        mRememberMe = (in.readByte() == 0 ? false : true);
        mAutoConnect = (in.readByte() == 0 ? false : true);
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public int describeContents() { 
        return 1; 
    } 
 
    /** {@inheritDoc} */ 
    @Override 
    public void writeToParcel(Parcel dest, int flags) {
        boolean[] validDataList = new boolean[MemberData.values().length];
        int validDataPos = dest.dataPosition();
        dest.writeBooleanArray(validDataList); // Placeholder for real array
 
        if (mUsername != null) {
            validDataList[MemberData.USERNAME.ordinal()] = true;
            dest.writeString(mUsername);
        } 
 
        if (mPassword != null) {
            validDataList[MemberData.PASSWORD.ordinal()] = true;
            dest.writeString(mPassword);
        } 
 
        if (mMobileNo != null) {
            validDataList[MemberData.MOBILE_NO.ordinal()] = true;
            dest.writeString(mMobileNo);
        } 
 
        if (mSubscriberId != null) {
            validDataList[MemberData.SUBSCRIBER_ID.ordinal()] = true;
            dest.writeString(mSubscriberId);
        } 
 
        dest.writeByte((byte)(mRememberMe ? 1 : 0));
        dest.writeByte((byte)(mAutoConnect ? 1 : 0));
 
        int currentPos = dest.dataPosition();
        dest.setDataPosition(validDataPos);
        dest.writeBooleanArray(validDataList); // real array
        dest.setDataPosition(currentPos);
    } 
 
    /*** 
     * Parcelable constructor for LoginDetails. 
     *