user4o01 user4o01 - 21 days ago 9
Android Question

Write a sub class of Parcelable to another Parcel

I have a class that implements

Parcelable
interface:

class A implements Parcelable {

}


I have another class
B
that contains an
A
object as an instance variable. In the
writeToPacel
inside class
B
, I want to write object
B
to the
Parcel
:

class B implements Parcelable{

public void writeToParcel(Parcel dest, int flags) {
dest.write ???
}
}


How can I write and read it?

Answer
class B implements Parcelable{
//lets assume you have A as a data member 

A obj;
public void writeToParcel(Parcel dest, int flags) {

        dest.writeParcelable(obj , flags);

    }
}

if you want to read it use this

 obj = in.readParcelable(A.class.getClassLoader());