Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Using Parcelable Data Using a Creator and Reading a Parcel

Laith Mohammad
Laith Mohammad
6,569 Points

The annoying CREATOR

Ok I understood that we "deCompile" the parcel this way:

    private Day(Parcel in){
        mTime = in.readLong();
        mSummary = in.readString();
        mTemperatureMax = in.readDouble();
        mIcon = in.readString();
        mTimeZone = in.readString();
    }

but what is this for?

public static final Creator<Day> CREATOR = new Creator<Day>() {
        @Override
        public Day createFromParcel(Parcel parcel) {
            return new Day(parcel);
        }

        @Override
        public Day[] newArray(int i) {
            return new Day[i];
        }
    };

I mean.. I'm totally confused here.. "create from parcel", then you pased Day as an argument.. agh.. my head got twisted.

then this, new Array??!!..

sorry but it would be better if Ben described this in more details.

Please help.

Thanks, Laith

1 Answer

Adam Sawicki
Adam Sawicki
15,967 Points

As I understand this Parcelable.Creator is an interface created to make it easy for system to recreate object of type that was put in the bundle.

The thing is that when you put your object type Foo in the bundle as parcelable the system calls method writeToParcel which every Parcelable object must have. On the other hand when the system wants to retrieve object from bundle it only knows that the object is type** Parcelable** it doesn't know that it is type Foo so it can't call proper constructor. The constructor must be called through the interface Parcelable.Creator, so that system can call method that you override in the interface implementation and system knows for sure that they are there.

I hope I helped you understand that, Regards