26

I am having 2 String arrays inside First Activity - A , now i need to pass both the arrays to the second_activity - B, how do i do it ?

I know about the Intent kind of concept in Android and already passed just single variable value to another activity, but i haven't implement the concept of passing string arrays between activities, i have already surfed net for the same.

pls let me know about the possible solution.

Paresh Mayani
  • 125,853
  • 70
  • 238
  • 294

3 Answers3

80
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);


Hope this will help you.

In order to read:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
viv
  • 6,118
  • 5
  • 34
  • 53
  • @viv thanx for the quick support, but can you please let me know about "key" ,is it a single value or an array ? pls – Paresh Mayani Dec 13 '10 at 13:04
  • it is just a name with which you want to catch your array in another activity...... ex: array1 or array2: so you will be catching it by this name only in other activity...... – viv Dec 13 '10 at 13:05
  • @viv ya i have done, what should be done to read bundle in Second activity ? – Paresh Mayani Dec 13 '10 at 13:08
  • 2
    this.getIntent().getExtras().getStringArray(key); – metter Dec 13 '10 at 13:09
  • @metter @viv thats really silly question by me , this is same way as we are sending and receiving single value,,,....by the way thanx a lot for quick, supportive and helpful answer – Paresh Mayani Dec 13 '10 at 13:13
  • To Bundle or not to Bundle, see here, http://stackoverflow.com/questions/15243798/advantages-of-using-bundle-instead-of-direct-intent-putextra-in-android – G O'Rilla Jul 20 '15 at 06:41
  • dont we have to add `startActivity(i);` – Rishav Jun 13 '17 at 18:48
5

Not directly an answer to the question but you can also use .putStringArrayListExtra() in your bundle. It is more flexible than sending string array.

Bundle b=new Bundle();
b.putStringArrayListExtra("URL_ARRAY_LIST",
                        myStringArrayList);
Intent i=new Intent(context, Class);
i.putExtras(b);

Then you can get this arrayList as follows:

ArrayList<String> urls;
urls = getIntent().getStringArrayListExtra("URL_ARRAY_LIST");
Illegal Argument
  • 9,707
  • 2
  • 40
  • 59
3

Intents carry data into a key-value map, where 'key' is a String name identifier that you choose when storing the data into the Intent. When reading that data, you request the same 'key'. You can store various data types in a single Intent.

ognian
  • 11,396
  • 4
  • 33
  • 33