Last active
April 20, 2021 02:22
-
-
Save patrickhammond/732d20b6e89fda23be1b to your computer and use it in GitHub Desktop.
A Parcelable is not always immediately deep copied; this forces an immediate deep copy into a new Parcelable instance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Parcel; | |
import android.os.Parcelable; | |
public class ParcelableHelper { | |
/** | |
* There is not always a guarantee that Parcelable values will be immediately written out and | |
* read back in. For data data that mutable (its own issue), this can be a problem. This is | |
* for the times when it would be great to have confidence that you will be working with a copy | |
* of that data. | |
*/ | |
public static Parcelable immediateDeepCopy(Parcelable input) { | |
return immediateDeepCopy(input, input.getClass().getClassLoader()); | |
} | |
/** | |
* Same as {@link #immediateDeepCopy(android.os.Parcelable)}, but for when you need a little | |
* more control over which ClassLoader will be used. | |
*/ | |
public static Parcelable immediateDeepCopy(Parcelable input, ClassLoader classLoader) { | |
Parcel parcel = null; | |
try { | |
parcel = Parcel.obtain(); | |
parcel.writeParcelable(input, 0); | |
parcel.setDataPosition(0); | |
return parcel.readParcelable(classLoader); | |
} finally { | |
if (parcel != null) { | |
parcel.recycle(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Parcel; | |
import android.os.Parcelable; | |
import junit.framework.TestCase; | |
public class ParcelableHelperTest extends TestCase { | |
public void testDeepCopySingleItem() { | |
TestParcelable originalItem = new TestParcelable(); | |
originalItem.someValue = "hello, world"; | |
TestParcelable copyItem = (TestParcelable) ParcelableHelper.immediateDeepCopy(originalItem); | |
assertNotSame(originalItem, copyItem); | |
assertNotSame(originalItem.someValue, copyItem.someValue); | |
} | |
public static class TestParcelable implements Parcelable { | |
public String someValue; | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
dest.writeString(this.someValue); | |
} | |
public TestParcelable() { | |
} | |
private TestParcelable(Parcel in) { | |
this.someValue = in.readString(); | |
} | |
public static final Creator<TestParcelable> CREATOR = new Creator<TestParcelable>() { | |
public TestParcelable createFromParcel(Parcel source) { | |
return new TestParcelable(source); | |
} | |
public TestParcelable[] newArray(int size) { | |
return new TestParcelable[size]; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment