Passing array with intents

Posted: September 12, 2011 in Android

 

 

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 didnt found anything so… Can anybody help me here? šŸ˜€

Thanxs!

 

 

8down voteaccepted

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");

Reference

http://stackoverflow.com/questions/3848148/sending-information-with-intent-putextra

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s