Search code examples
androidandroid-fragmentsbundleandroid-contentproviderparcelable

Android, pass a parcelable array or fetch data again from Content Provider?


Bit of a performance question. I have a fragment that fetched some data from the content provider and built an object which implements Parcelable.

MyObject object;

In the scenario where such objects is required in another fragment, what would be more convenient: pass the object as

args.putParcelable(ARG_KEY_OBJECT, object);

or pass the id of the object so that the new fragment can fetch it again from the content provider?

args.putString(ARG_KEY_OBJECT_ID, object.getId());

Furthermore: what if we're talking about a list of those objects?

ArrayList<MyObject> list = ....
args.putParcelableArrayList(ARG_KEY_OBJECT_LIST, list);

Solution

  • It turns out it is faster to pass the object as a parcelable. At the same time, this might not be the most convenient solution. For instance, passing the object ID and retrieving again in the new fragment is very handy if used in combination with an async loader, which can trigger callbacks if the stored object in the content provider changes.