11

How can I pass ArrayList from one activity to other activity?

think shra1
  • 239
  • 2
  • 4
  • 10

5 Answers5

42

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");
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
4

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);
Anand
  • 1,315
  • 1
  • 12
  • 18
0

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.

najczuk
  • 133
  • 1
  • 8
0

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

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
  • 5
    Using static fields to transfer data from one activity to another is a really bad design :/ – Valentin Rocher Jan 24 '11 at 10:47
  • And why so!? if only a small number of values are concerned!? – Aman Alam Jan 24 '11 at 10:59
  • 7
    Because 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
  • 2
    Thanks! 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
  • 1
    Well @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
-2

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");
}
CodeBanBan
  • 48
  • 5