Android ArrayList pass as parcelable

I know this question is rather old but since I originally came here looking for answers, I wanted to share my experience.

Yes, you need to implement Parcelable for your Locality class but that is it.

If your LocalityList is ONLY a wrapper for ArrayList, then you do not need it.

Just use the putParcelableArrayList method.

ArrayList<Locality> localities = new ArrayList<Locality>;
...
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities);
fragmentInstance.setArguments(bundle);

return fragmentInstance;

And retrieve it using…

localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST);

So, unless you need the custom ArrayList for some other reason, you can avoid doing any of that extra work and only implement Parcelable for your Locality class.

Leave a Comment