Android ArrayList<MyObject> pass as parcelable

谁说我不能喝 提交于 2019-11-27 13:57:08

Yes, make Locality class itself Parcelable, and don't forgot to initialize

ArrayList<Locality> mList= new ArrayList<Locality>();

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.

The trick i normally use is to parse the list to Json using Gson (from google). On the other side i just parte the string in Json back to a new list.

Never noticed any lag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!