Question

I have a class called SuperMedia that implements Parcelable. One of the fields of the class is an ArrayList children. When I create a Bundle and try to pass a "SuperMedia" object from one activity to another, all the fields get passed fine with the exception of the ArrayList "children" which just shows up as being empty every time.

In my first Activity I do:

 Bundle a = new Bundle();
 a.putParcelable("media",media); //media is an object of type "SuperMedia" and all the "children" have been initialized and added to the array
 final Intent i = new Intent("com.tv.video.subcategories");
 i.putExtra("subcategories", a);

On my Second Activity I do:

  Intent i = getIntent();
  Bundle secondBun = i.getBundleExtra("subcategories");
  SuperMedia media = secondBun.getParcelable("media"); //For some reason the ArrayList"children" field shows up as empty. 

Im not sure why this is happening. If anybody can guide me on the right path that would be greatly appreciated. Below is my SuperMedia class btw.

public class SuperMedia implements Parcelable{

public URI mthumb;
public String mTitle;
public ArrayList<SuperMedia> children = new ArrayList(); 

public SuperMedia(URI thumb, String title) {
    this.mthumb = thumb;
    this.mTitle = title;
}


@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeString(mTitle);
    dest.writeString(mthumb.toString());
    dest.writeTypedList(children);

}

public static final Parcelable.Creator<SuperMedia> CREATOR = new Parcelable.Creator<SuperMedia>() {
    public SuperMedia createFromParcel(Parcel in) {
        return new SuperMedia(in);
    }

    public SuperMedia[] newArray(int size) {
        return new SuperMedia[size];
    }
};

private SuperMedia(Parcel in) {
    mTitle = in.readString();
    try {
        mthumb = new URI(in.readString());
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    in.readTypedList(children, SuperMedia.CREATOR);

}

public SuperMedia(){

}

}

Was it helpful?

Solution

If you want simply pass object through intent then you can make SuperMedia Serializable no need to Parcelable.

public class SuperMedia implements Serializable{...}

put it as

Bundle a = new Bundle(); a.putSerializable("media",media);

and we get it as.

Intent i = getIntent();
Bundle secondBun = i.getBundleExtra("subcategories");
SuperMedia media = (SuperMedia)secondBun.getSerializable("media");

if you really needed Parcelable then may it help you. Arraylist in parcelable object

OTHER TIPS

Use Bundle's putParcellableArrayList for storing SuperMedia object

Bundle args = new Bundle();
args.putParcelableArrayList("media", "media");

and for restroring

getArguments().getParcelableArrayList("media");

This way will ensure bundle save your list objects as implemented in parcelable instance. Also, be aware of using only ArrayList, other List subclasses not supported.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top