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

柔情痞子 提交于 2019-12-07 17:26:58

问题


I am trying to share data between two applications.. first I thought of saving a file to SD Card then read it.. but this solution won't work.. so I wan woundering if there is a way to send an ArrayList of an Object that implements Parcelable..

what other way could be followed to achieve this?

NOTE:
Activities are not within the same application.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/12493818/how-to-pass-arraylistcustomobject-to-an-activity-in-another-application

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