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
  • 127,700
  • 71
  • 241
  • 295

4 Answers4

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,158
  • 6
  • 39
  • 54
  • @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
  • 10,090
  • 2
  • 44
  • 61
4

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,451
  • 4
  • 35
  • 33
0

To send arrayList Data to another activity,

  • create intent in first activity
  • get data using bundle in second activity

(1) Code to create intent in first activity:

Intent intent = new Intent(firstActivityName.this, SecondActivityName.class);

intent.putExtra("Array", ArrayListName);

startActivity(intent);

(2) Code to get data using bundle in second activity:

Bundle bundle = getIntent().getExtras();

ArrayList<String> arrayList = new ArrayList<>();

arrayList = (ArrayList <String>) bundle.getSerializable("Array");
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Marcel Hofgesang Sep 27 '22 at 07:26
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32782816) – abdo Salm Sep 28 '22 at 15:39