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?

  • What are you trying to achieve? - katso
  • don't like Serializable , use Parcelable ) - ermak0ff
  • Pass in the fragment constructor - Werder
  • I open the fragment, I need to transfer data to it. Several ArrayList, a pair of String and a link to where to return the result. - Igor
  • Parcelable do, plus for all objects inside implement Parcelable. It's not difficult there, the eyes are afraid - the hands are doing. - Alexey Malchenko

1 answer 1

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