0

I would like to pass List<String[]> through intent to activity and then retrieve it. Anyone know a way on how to do it properly ? Thank you

Datenshi
  • 1,191
  • 5
  • 18
  • 56
  • Possible duplicate of [Intent.putExtra List](http://stackoverflow.com/questions/6543811/intent-putextra-list) – cammando Jan 26 '17 at 08:05

3 Answers3

5

I would put it in a serializable and then pass the serializable object in the bundle to the next activity.

Bundle bundle = new Bundle();
bundle.putSerializable("list", serializableList);

mainIntent.putExtras(bundle);
startActivity(mainIntent);

java.util.ArrayList already implements the Serializable interface. so that would be perfect for your purposes. Then on the other Activity you can use the following code to retrieve the list

Bundle bundle = getIntent().getExtras();
userInfo = (ArrayList) bundle.getSerializable("list");

Hope it helps.

greenkode
  • 4,001
  • 1
  • 26
  • 29
-1

Put your variable in a static property of an object.

ex.

 public class Util {

     public static List<String[]>  mystaticlist;

}

and access it statically from the second activity:

    List<String[]>  mystaticlist = Util.mystaticlist;
Gyonder
  • 3,674
  • 7
  • 32
  • 49
-1

It's possible, but you need to pass it as a Serializable and you'll need to cast the result when you extract the extra. Since ArrayList implements Serializable and String[] is inherently serializable, the code is straightforward. To pass it:

ArrayList<String[]> list = . . .;
Intent i = . . .;

i.putExtra("strings", list);

To retrieve it:

Intent i = . . .;
ArrayList<String[]> list = (ArrayList<String[]>) getSerializableExtra("strings");
K_Anas
  • 31,226
  • 9
  • 68
  • 81