How to pass ArrayList<CustomObject> to an activity in another application?

╄→尐↘猪︶ㄣ 提交于 2019-12-06 03:04:36

ArrayList is a Serializable class and as such can be placed into an Intent so long as your custom object is also Serializable. It'd look something like:

Intent intent = new Intent(YourActivity.this, YourReceiverActivity.class);
intent.putExtra("YourArrayList", new ArrayList<YourSerializableObject>());
startActivity(intent);

Then retrieve it:

Intent intent = getIntent();
ArrayList<YourSerializableObject> list = 
    (ArrayList<YourSerializableObject>)intent.getSerializableExtra("YourArrayList");

For your object to be Serializable, all of the contained members need to be Serializable as well.

jmhostalet

You may use a static class (called DataChannel for example) with a static member kind of ArrayList< CustomObject>, then use static get/set to pass the object between activities.

I'm not sure why no one mentioned this but your best bet is BY FAR SQLite. It's MUCH more efficient than any object passing as it does not hog any memory in comparison.

You could store the array of objects as an extra in an Intent using :

public Intent putExtra (String name, Parcelable[] value)

You have to configure your other app to receive an Intent for your custom URI.

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