How to pass Collections like ArrayList
, etc from one Activity
to another as we used to pass Strings
, int
with help of putExtra method of Intent?
Can anyone please help me as i want to pass a List<String>
from one Activity
to another?
How to pass Collections like ArrayList
, etc from one Activity
to another as we used to pass Strings
, int
with help of putExtra method of Intent?
Can anyone please help me as i want to pass a List<String>
from one Activity
to another?
You can pass an
ArrayList<E>
the same way, if theE
type isSerializable
.You would call the
putExtra (String name, Serializable value)
ofIntent
to store, andgetSerializableExtra (String name)
for retrieval.Example:
In the other Activity:
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).
To pass ArrayList from one activity to another activity, one should include
before starting the activity.And to get the ArrayList in another activity include
you will be able to pass ArrayList through this.Hope this will be helpful to you.
use
putExtra
to pass value to an intent. usegetSerializableExtra
method to retrieve the data like thisActivity A :
Activity B:
First you need to create a Parcelable object class, see the example
And the list
Code from Calling activity
Code on called activity
I tried all the proposed techniques but none of them worked and stopped my app from working and then I finally succedded. Here is how I did it... In main activity I did this:
To get the data in the new Activity:
I hope this will help someone...