Passing a List from one Activity to another
Solution 1
You can pass an ArrayList<E>
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<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) 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).
Solution 2
First you need to create a Parcelable object class, see the example
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
}
public Student(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
And the list
ArrayList<Student> arraylist = new ArrayList<Student>();
Code from Calling activity
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
Code on called activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
Solution 3
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:
List<String> myList...;
Intent intent = new Intent...;
Bundle b=new Bundle();
b.putStringArrayList("KEY",(ArrayList<String>)myList);
intent_deviceList.putExtras(b);
....startActivity(intent);
To get the data in the new Activity:
List<String> myList...
Bundle b = getIntent().getExtras();
if (b != null) {
myList = bundle.getStringArrayList("KEY");
}
I hope this will help someone...
Solution 4
use putExtra
to pass value to an intent. use getSerializableExtra
method to retrieve the data
like this
Activity A :
ArrayList<String> list = new ArrayList<String>();
intent.putExtra("arraylist", list);
startActivity(intent);
Activity B:
ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
Solution 5
To pass ArrayList from one activity to another activity, one should include
intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass
before starting the activity.And to get the ArrayList in another activity include
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList
}
you will be able to pass ArrayList through this.Hope this will be helpful to you.
Name is Nilay
Working as an Android Application Developer in Ahmedabad, Gujarat.
Updated on August 02, 2021Comments
-
Name is Nilay almost 3 years
How to pass Collections like
ArrayList
, etc from oneActivity
to another as we used to passStrings
,int
with help of putExtra method of Intent?Can anyone please help me as i want to pass a
List<String>
from oneActivity
to another? -
Jon Taylor almost 12 years+1 for mentioning that E has to be Serializable, something the others seem to have forgotten to mention.
-
Murali Ganesan over 9 yearsHi RajaReddy, I pass array list A activity to B activity. B activity i have changed one value and come back to A activity value not changing. Please help me
-
RajaReddy PolamReddy over 9 yearsWhile coming back to B Activity you have to pass data From B to A.. or make that array as global array and update from B activity .
-
Murali Ganesan over 9 yearsIn my case i have option to go C activity. In that scenario how can i update the arraylist?
-
RajaReddy PolamReddy over 9 yearsThen make it as Global in Application class... and update and use where ever you want to use
-
Murali Ganesan over 9 yearsThanks for your tips. If possible give me ur mail id, I will ask doubts through email.
-
Pankaj Arora about 8 yearsWorking like a charm, after an hour i got this, many thanks buddy.
-
SqueezyMo almost 8 yearsOne thing worth mentioning is,
List
interface does not extendSerializable
so if your collection is declared by interface you have to cast it to a specific implementation first. -
Harish Reddy almost 7 yearsIts better to use serializable then this one, But Its good for someone. +1
-
Expert wanna be over 6 years@HarishReddy Could you explain why using serializable is better? I thought Parcelable is better than serializable for the performance.
-
Mashhood almost 6 yearsAs it works on order basis as values are given in
writeToParcel
. Is there any mechanism to get values fromParcel
object on key basis?. For example if in class we have multiple string fields then how can we come to know which string we are getting fromParcel
inStudent(Parcel in)
. -
stevehs17 over 5 years@HarishReddy: Using Serializable allows for a simpler implementation, but using Parcelable provides better performance. Which one is better depends on the circumstances: if the performance Serializable provides in your use case is adequate, then you should prefer the simpler approach. But if using Serializable is too slow in your use case, you should try using Parcelable instead.
-
iamkdblue almost 5 yearsnote :- intent.putExtra("mylist", (Serializable)myList); worked for me !
-
MindRoasterMir over 3 years@iamkdblue . your solution worked and accepted answer did not. I have a list of my own Class have a key and value. It works only for simple String List. Thanks
-
iamkdblue over 3 years@MindRoasterMir you're welcome, no it's also worked for a custom class List
-
MindRoasterMir over 3 years@iamkdblue You are write and I also said the same, may be my comment is not formulated correctly. I am saying the accepted asnwer does not work for a custom class List, and it is also to mention that the custom class must implement Serializable otherwise it wont work. Thanks
-
Kumza Ion almost 3 years@Shahin ShamS Could you please have a look at this question. I tried to implement your solution there: stackoverflow.com/questions/68050680/…