4

I have a problem to pass an integer array ​​between two activities. I tried this code:

ActivityA:

Bundle myBundle = new Bundle(); 
myBundle.putIntArray("myarray", array);
startActivity(intent);

ActivityB:

Bundle myBundle = getIntent().getExtras();
int[] myIntArray = myBundle.getIntArray("myarray");

I don't understand why i can't use array values in activityB. Can you help me, please? Thanks

Cœur
  • 37,241
  • 25
  • 195
  • 267
bisemanu
  • 441
  • 2
  • 9
  • 19

2 Answers2

4

Activity B:

Bundle bundle = getIntent().getExtras();

the do your code.

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
3

You are never adding the bundle to the Intent in Activity A. In addition, since you are passing an array of integers, you can add them directly to the intent. Like this:

intent.putExtra("myarray", array);
startActivity(intent);

Then you can easily retrieve it in Activity B like this:

int[] myIntArray = getIntent().getIntArrayExtra("myarray");
theisenp
  • 8,639
  • 5
  • 39
  • 47