How to transfer object link to new fragment? I try through Bundle - there I need to transfer a Serializable object, and how can I do without it?
|
1 answer
Here is an example of how to make lists with Parcelable:
class TestA implements Parcelable { String field1; List<TestB> testBList; protected TestA(Parcel in) { field1 = in.readString(); testBList = in.createTypedArrayList(TestB.CREATOR); } public static final Creator<TestA> CREATOR = new Creator<TestA>() { @Override public TestA createFromParcel(Parcel in) { return new TestA(in); } @Override public TestA[] newArray(int size) { return new TestA[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(field1); dest.writeTypedList(testBList); } } class TestB implements Parcelable { String field1; protected TestB(Parcel in) { field1 = in.readString(); } public static final Creator<TestB> CREATOR = new Creator<TestB>() { @Override public TestB createFromParcel(Parcel in) { return new TestB(in); } @Override public TestB[] newArray(int size) { return new TestB[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(field1); } } - Thank. But I decided to simply add implements Serializable to the class without redefining the methods and passed it through the Bundle. - Igor
- As far as I know more correctly in such cases use Parcelable. Although if you have the usual POJO, then you can and so leave. - Alexey Malchenko
|
Serializable, useParcelable) - ermak0ff