0

I want to send String and List<String> from one activity to another activity using the following code in first activity:

Intent intent1=new Intent(Activity2);
Bundle b=new Bundle();
b.putStringArrayList("IDs", (ArrayList<String>) ids);
intent1.putExtra("message","IDs");
intent1.putExtras(b);

And I am accessing this content in second activity as follows:

 Bundle b= intent.getExtras();
 String str=b.getString("message");
 ArrayList<String> list=b.getStringArrayList("IDs");

The problem is: I am getting str as null even though list is getting correct values. Am I missing something in using the Bundle?

Kiran
  • 747
  • 2
  • 10
  • 35
  • Please explain your problem better. You have provided contradictory information. In your title, you state "Unable to pass String and List", yet in your description, you state "list" is getting correct values". Which is it? Does the List parameter make it to the 2nd activity or not? – EJK Oct 22 '13 at 06:42
  • @Kiran see this example http://stackoverflow.com/questions/4303800/how-to-store-and-retrieve-array-list-value-in-bundle-in-android – Karthikeyan Karthik Oct 22 '13 at 06:42
  • I think this has been answered here -> [http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity][1] [1]: http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity – MrTimpi Oct 22 '13 at 06:44
  • @EJK Hi, I am sorry for that. I am sending both at the same time but only List is received. – Kiran Oct 22 '13 at 06:51

3 Answers3

2

You need to do

Intent intent1=new Intent(Activity2);
Bundle b=new Bundle();
b.putStringArrayList("IDs", (ArrayList<String>) ids);
b.putExtra("message","IDs");
intent1.putExtras(b);

Instead of using

Intent intent1=new Intent(Activity2);
Bundle b=new Bundle();
b.putStringArrayList("IDs", (ArrayList<String>) ids);
intent1.putExtra("message","IDs");
intent1.putExtras(b);

The reason is putExtras replaces all the previous value which is set to Intent.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

What you are doing wrong is, You are passing 'message' as intent and trying to get it from bundle

intent1.putExtra("message","IDs");

Instead use

b.putString("message","IDs");
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

Send the extar parameter like this

            Intent intent1 = new Intent(this, Activity2.class);

        searchUserIntent.putStringArrayListExtra("IDs", ids);

        startActivity(searchUserIntent);

And to get the intent parameters use

            Intent intent2 = getIntent();
    ids2 = intent2.getStringArrayListExtra("IDs");
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32