How can I pass ArrayList
from one activity to other activity?
5 Answers
It depends on the type of arraylist
putIntegerArrayListExtra(String name, ArrayList<Integer> value)
putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
putStringArrayListExtra(String name, ArrayList<String> value)
putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)
Then you can read from you next activity by replacing put
with get
with key string as argument,eg
myIntent.getStringArrayListExtra("arrayPeople");

- 34,521
- 28
- 94
- 112
-
@LabeebP How to pass ArrayList – Harish Koona Mar 06 '14 at 06:58
You can create one bundle in bundle put parceable array list provided by labeeb and set to intent here is the code for
Intent i = new Intent(this,name.class);
Bundle b = new Bundle();
b.putIntegerArrayListExtra(String name, ArrayList<Integer> value);
//b.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value);
//b.putStringArrayListExtra(String name, ArrayList<String> value);
i.putExtra(String name,b);
startActivity(i);
And get data into another activity like
//pseudo code
Bundle b = getIntent().getExtra().putParcelableArrayListExtra(String name);

- 1,315
- 1
- 12
- 18
In revoked Activity you should use
Bundle bundle = getIntent().getExtras();
ArrayList<String> stringArray = bundle.getStringArrayList(ParentActivity.STRING_LIST);
where ParentActivity.STRING_LIST
is your key constant for the list.

- 133
- 1
- 8
According to me, create static class and put your array-list in it while you traverse from one activity to the other.
When you reach in another activity, access the value that you stored in the static class.
UPDATE
I've learned with time that it's a horrible practice. When the objects are wiped/recreated the Static values might be lost. and then we put a lot of data in memory too. Using anything like Parcelable is a good practice

- 11,231
- 7
- 46
- 81
-
5Using static fields to transfer data from one activity to another is a really bad design :/ – Valentin Rocher Jan 24 '11 at 10:47
-
-
7Because when the user does things like go BACK or onPause() etc. the app still thinks the variables are in memory when they aren't and the app FCs. I learnt the hard way :-) – Infiniti Fizz Apr 26 '11 at 19:18
-
2Thanks! I am also learning it the hard way :p See that 3 downvotes! :( – Aman Alam Apr 27 '11 at 08:10
-
@ValentinRocher Can you please explain me why its bad to pass the data in static field from one activity to another.If you have any link then its well and good for me. – Narendra Pal Sep 21 '12 at 03:43
-
@InfinitiFizz Can you please explain me why its bad to pass the data in static field from one activity to another.If you have any link then its well and good for me. – Narendra Pal Sep 21 '12 at 03:43
-
1Well @nick its my answer and I've also learned that it's a horrible practice. When the objects are wiped/recreated the Static values might be lost. and then we put a lot of data in memory too. Using anything like Parcelable is a good practice – Aman Alam Dec 06 '12 at 13:37
When you create intent. you can set data by
intent.putExtra("keyName", "somevalue");
when intent B start you can get data by
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}

- 48
- 5
-
-
im unable to get those values...im geting null value... thanks in advance – think shra1 Jan 24 '11 at 10:40