下面列出了怎么用android.os.Parcelable.Creator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Converts a {@link ByteString} into a {@link Parcelable}.
*
* @param byteString the {@link ByteString} encoding of the {@link Parcelable}
* @return an instance of {@link Parcelable}
*/
@Override
public Parcelable convert(@NonNull ByteString byteString) {
Parcel parcel = Parcel.obtain();
byte[] bytes = byteString.toByteArray();
Parcelable fromParcel = null;
try {
parcel.unmarshall(bytes, 0, bytes.length);
// Move the current read/write position to the beginning
parcel.setDataPosition(0);
Field creatorField = parcelableClass.getField("CREATOR");
Parcelable.Creator<?> creator = (Creator) creatorField.get(null);
fromParcel = parcelableClass.cast(creator.createFromParcel(parcel));
} catch (NoSuchFieldException nsfe) {
throw new RemoteProtocolException(
String.format(
Locale.ROOT,
"Cannot find CREATOR field for Parcelable %s",
parcelableClass.getName()),
nsfe);
} catch (IllegalAccessException iae) {
throw new RemoteProtocolException(
String.format(
Locale.ROOT,
"Cannot create instance of %s. CREATOR field is inaccessible",
parcelableClass.getName()),
iae);
} finally {
if (parcel != null) {
parcel.recycle();
}
}
return fromParcel;
}
private static <T> Parcelable.Creator<T> newCreator(final Class<T> clazz) {
return new Parcelable.Creator<T>() {
@Override
public T createFromParcel(Parcel source) {
return readFromParcel(clazz, source);
}
@Override
public T[] newArray(int size) {
return Postman.newArray(clazz, size);
}
};
}
public static <T> Creator<T> newCreator(ParcelableCompatCreatorCallbacks<T> callbacks) {
if (VERSION.SDK_INT >= 13) {
return ParcelableCompatCreatorHoneycombMR2Stub.instantiate(callbacks);
}
return new CompatCreator(callbacks);
}
public void recreatesEqual(Creator<T> creator) {
T recreated = forceParcel(actual, creator);
check("recreatesEqual()").that(actual).isEqualTo(recreated);
}
/**
* Parcelables are lazily marshalled, meaning that in typical testing, no marshalling would occur
* and would therefore go untested. This forces marshalling to happen for a Parcelable.
*
* <p>This utility will marshall the provided Parcelable, and attempt to recreate it with the
* given CREATOR. It is up to the caller to validate the two instances are equivalent.
*
* @param parcelable the parcelable to marshall.
* @param creator the CREATOR field for that parcelable.
* @return a new instance of the parcelable that has been unmarshalled.
*/
public static <T extends Parcelable> T forceParcel(T parcelable, Creator<T> creator) {
Parcel parcel = Parcel.obtain();
try {
parcelable.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
return creator.createFromParcel(parcel);
} finally {
parcel.recycle();
}
}
/**
* Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link
* Parcelable}.
*
* @param clazz The class associated with the type the the Creator will create.
* @param <T> The type the Creator will create.
*
* @return A fully implemented Creator for the specified type.
*
* @throws PostmanException if there is no {@link Parceler} associated with the given type.
*/
public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException {
@SuppressWarnings("unchecked")
Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz);
if (creator == null) {
creator = newCreator(clazz);
creatorMap.put(clazz, creator);
}
return creator;
}