Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a String array value from one activity to another Activity in android?

Tags:

android

I am having two activities in my application. I want to pass tha array of String from one activity to another.. How to pass this values from activity to activity?

like image 259
Thiru Avatar asked Jan 17 '12 10:01

Thiru


People also ask

How do I pass a string from one activity to another in Android?

putExtra() method is used for sending the data, data in key-value pair key is variable name and value can be Int, String, Float, etc. getStringExtra() method is for getting the data(key) that is sent by the above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()


4 Answers

You can consider using Intent.getStringArrayExtra

In the first activity:

Intent intent = new Intent(context, NewActivity.class);
intent.putExtra("string-array", stringArray);
context.startActivity(intent);

and in the second one:

Intent intent = getIntent();
String [] stringArray = intent.getStringArrayExtra("string-array");
like image 76
kgiannakakis Avatar answered Sep 25 '22 09:09

kgiannakakis


Here's some reading: http://www.vogella.de/articles/AndroidIntent/article.html#overview_accessdata go to section 2.1.

Also, How to pass ArrayList using putStringArrayListExtra() should explain something similar.

like image 23
AJcodez Avatar answered Sep 21 '22 09:09

AJcodez


just serialize it and set it in the extras of the intent (of activity) you wanna open.
You will receive it in the onCreate() of that activity.
Convert it to array again.

like image 35
akkilis Avatar answered Sep 24 '22 09:09

akkilis


In Activity One, write this code to create an array and pass it to another activity:

String[] array1={"asd","fgh","dcf","dg","ere","dsf"};
Intent i=new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("key",array1);
startActivity(i);

In Second Activity, write this to retrieve your array

String[] array = getIntent().getStringArrayExtra("key"); 
like image 41
Ashish Kapoor Avatar answered Sep 23 '22 09:09

Ashish Kapoor