26

I am having two 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.

What might be a possible solution?

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

7 Comments

@viv thanx for the quick support, but can you please let me know about "key" ,is it a single value or an array ? pls
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 ya i have done, what should be done to read bundle in Second activity ?
this.getIntent().getExtras().getStringArray(key);
@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
|
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");

7 Comments

Yes it's not an answer of the question exactly but yes we can use this alternate solution. BTW, question is almost 4 years old :-)
@PareshMayani I know that it is old I need to pass string data across two activities that was dynamic so string array would be inefficient as we could not alter the size this was the solution to my question and wanted to share it.
@PareshMayani after four years did you come across any new solution?
@HabeebPerwad check accepted and other answers! Solutions are already provided there.
@HabeebPerwad there is no convienence method for that right now have a look at Parceable in android
|
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.

1 Comment

ya i was making silly mistake while asking a question regarding "key", by the way thanx for the support
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");

2 Comments

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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.