Sending arrays with Intent.putExtra

Kitinz picture Kitinz · Oct 3, 2010 · Viewed 141.6k times · Source

I have an array of integers in the activity A:

int array[] = {1,2,3};

And I want to send that variable to the activity B, so I create a new intent and use the putExtra method:

Intent i = new Intent(A.this, B.class);
i.putExtra("numbers", array);
startActivity(i);

In the activity B I get the info:

Bundle extras = getIntent().getExtras();
int arrayB = extras.getInt("numbers");

But this is not really sending the array, I just get the value '0' on the arrayB. I've been looking for some examples but I didn't found anything so.

Answer

Mark B picture Mark B · Oct 3, 2010

You are setting the extra with an array. You are then trying to get a single int.

Your code should be:

int[] arrayB = extras.getIntArray("numbers");