Unmarshall a parcel object from a byte array. - Android Android OS

Android examples for Android OS:Parcel

Description

Unmarshall a parcel object from a byte array.

Demo Code


//package com.java2s;
import android.os.Parcel;
import android.os.Parcelable;

public class Main {
    /**/*from   w  ww .ja  va 2  s. c  o  m*/
     * Unmarshall a parcel object from a byte array.
     * @param bytes
     * @return
     */
    public static Parcel unmarshall(byte[] bytes) {
        Parcel parcel = Parcel.obtain();
        parcel.unmarshall(bytes, 0, bytes.length);
        parcel.setDataPosition(0); // this is extremely important!
        return parcel;
    }

    /**
     * Unmarshall a parcelable instance from a byte array.
     * @param bytes
     * @param creator
     * @param <T>
     * @return
     */
    public static <T> T unmarshall(byte[] bytes,
            Parcelable.Creator<T> creator) {
        Parcel parcel = unmarshall(bytes);
        return creator.createFromParcel(parcel);
    }
}

Related Tutorials