17

I need to pass an array of String/integer values from one Activity to another. How do I achieve this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
chetan
  • 1,385
  • 4
  • 15
  • 31

5 Answers5

36

In activity A:

String[] abc;

Bundle bundle =new Bundle();
bundle.putStringArray("some string",abc);

In Activity B where you want to get give the code as:

String abcd[]=bundle.getStringArray("some string");

"some string" should be same in both case.

assylias
  • 321,522
  • 82
  • 660
  • 783
Niki
  • 1,161
  • 1
  • 20
  • 37
  • hey niki thanks!!...but have u ever tried this because the various sites i have visited have mentioned that u can not pass array using a bundle...is that true?? – chetan Mar 14 '11 at 14:40
5

At the sender side, the code should be:

String[] myStrings=new String[2];
myStrings[0]="MONDAY";
myStrings[1]="TUESDAY";
Intent intent = new Intent(v.getContext(), Animation_program.class);
Bundle bundle = new Bundle();
intent.putExtra("strings", myStrings);
intent.putExtras(bundle);               
startActivity(intent);

At the reciever side, the code should be:

Intent i = getIntent();
Bundle extras=i.getExtras();

if(extras != null)  //this line is necessary for getting any value
{
    String[] fajr_Values = i.getStringArrayExtra("strings");
    Toast.makeText(this, "value="+fajr_Values[0]+""+fajr_Values[1], Toast.LENGTH_SHORT).show();
}
ormaaj
  • 6,201
  • 29
  • 34
2

I have never passed an array using a bundle, and I do not know off the top of my head if it can be done, but you can certainly pass an ArrayList (or anything Serializable/Parcelable). See this question for a more complete answer:

Passing data of a non-primitive type between activities in android

Community
  • 1
  • 1
Cephron
  • 1,577
  • 1
  • 19
  • 26
1

Refer this pass arraylist from one activity to other may help you

Community
  • 1
  • 1
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • Plz check: https://stackoverflow.com/questions/55350646/ I fail to pass string array data to next swipe view fragment – achal naskar Mar 26 '19 at 13:17
-2

Code for pass String And Integer value ::

In Your First Activity ::

Intent intent = new Intent(California.this,details.class);
Bundle bundle = new Bundle();
bundle.putString("Keyname1", StringValue);
bundle.putInt("Keyname2", IntegerValue);
intent.putExtras(bundle);
startActivity(intent);

In Second Activity :

Bundle b=this.getIntent().getExtras();
String s=b.getString("Keyname1");
int i=b.getInt("Keyname2");
Community
  • 1
  • 1
Mitul Goti
  • 2,657
  • 1
  • 22
  • 19