Full code looks like
MyInterface
public interface MyInterface extends Parcelable {
}
MyImplementation1
public class MyImplementation1 implements MyInterface, Parcelable {
public final int var1;
public MyImplementation1() {
this.var1 = 1;
}
protected MyImplementation1(Parcel in) {
var1 = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(var1);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<MyImplementation1> CREATOR = new Creator<MyImplementation1>() {
@Override
public MyImplementation1 createFromParcel(Parcel in) {
return new MyImplementation1(in);
}
@Override
public MyImplementation1[] newArray(int size) {
return new MyImplementation1[size];
}
};
}
MyImplementation2
public class MyImplementation2 implements MyInterface, Parcelable {
public final int var2;
public MyImplementation2(int var2) {
this.var2 = var2;
}
protected MyImplementation2(Parcel in) {
var2 = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(var2);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<MyImplementation2> CREATOR = new Creator<MyImplementation2>() {
@Override
public MyImplementation2 createFromParcel(Parcel in) {
return new MyImplementation2(in);
}
@Override
public MyImplementation2[] newArray(int size) {
return new MyImplementation2[size];
}
};
}
MyClass
public class MyClass implements Parcelable {
private final MyInterface var1;
private final List<MyInterface> var2;
public MyClass(MyInterface var1, List<MyInterface> var2) {
this.var1 = var1;
this.var2 = var2;
}
protected MyClass(Parcel in) {
var1 = in.readParcelable(MyInterface.class.getClassLoader());
var2 = in.readArrayList(MyInterface.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(var1, flags);
dest.writeTypedList(var2);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<MyClass> CREATOR = new Creator<MyClass>() {
@Override
public MyClass createFromParcel(Parcel in) {
return new MyClass(in);
}
@Override
public MyClass[] newArray(int size) {
return new MyClass[size];
}
};
}
Using it
MainActivity1
public class MainActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_1);
List<MyInterface> list = new ArrayList<>(2);
list.add(new MyImplementation2(21));
list.add(new MyImplementation2(22));
MyClass myClass = new MyClass(new MyImplementation1(), list);
Intent intent = new Intent(this, MainActivity2.class);
Bundle b = new Bundle();
b.putParcelable("key", myClass);
intent.putExtras(b);
startActivity(intent);
}
}
MainActivity2
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
Bundle b = getIntent().getExtras();
MyClass myClass = b.getParcelable("key");
}
}