Passing a List from one Activity to another

后端 未结 5 2130
花落未央
花落未央 2020-12-01 13:09

How to pass Collections like ArrayList, etc from one Activity to another as we used to pass Strings, int with help of put

5条回答
  •  借酒劲吻你
    2020-12-01 13:33

    You can pass an ArrayList the same way, if the E type is Serializable.

    You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

    Example:

    ArrayList myList = new ArrayList();
    intent.putExtra("mylist", myList);
    

    In the other Activity:

    ArrayList myList = (ArrayList) getIntent().getSerializableExtra("mylist");
    

    Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).

提交回复
热议问题