0

I'm new in programming and I recently encountered a problem in activities. I couldn't pass my int array to multiple activities.

Here is my First Activity :

 public void Start(View view) {
    Intent i = new Intent(this, Situation1.class);
    i.putExtra("arr", new int[]{0, 0, 0, 0, 0, 0});
    startActivity(i);
}

Here is my Situation1:

 public void Serious(View view)
{
    Intent intent = new Intent(this, Situation2.class);
    Intent i = getIntent();
    int[] arr = getIntent().getIntArrayExtra("arr");
    arr[2]=arr[2]+1;
    arr[1]=arr[1]+1;
    startActivity(intent);
}

Situation2:

public void India(View view)
    {
    Intent intent = new Intent(this, Situation3.class);
    Intent i = getIntent();
    Bundle b = i.getExtras();
    int[] arr=i.getIntArrayExtra("arr");
    arr[0]=arr[0]+1;
    startActivity(intent);
}

Thank you for the help!

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
S.Go
  • 1
  • 3

3 Answers3

1

In Situation1 and Serious function, you didn't put your array in intent which you start the next activity with.

public void Serious(View view)
{
Intent intent = new Intent(this, Situation2.class);
Intent i = getIntent();
int[] arr = getIntent().getIntArrayExtra("arr");
arr[2]=arr[2]+1;
arr[1]=arr[1]+1;
intent.putExtra("arr", arr);
startActivity(intent);
}
Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
0

You can use arraylist of integer like this:

This code sends array of integer values

Initialize array List

List<Integer> test = new ArrayList<Integer>();

Add values to array List

test.add(1);
test.add(2);
test.add(3);
Intent intent=new Intent(this, targetActivty.class);

Send the array list values to target activity

intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test);
startActivity(intent);

here you get values on targetActivty

Intent intent=getIntent();
ArrayList<String> test = intent.getStringArrayListExtra("test");

For more information:

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

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33
0

While passing:

Bundle b=new Bundle();
b.putIntArray("arr", new int[]{0, 0, 0, 0, 0, 0});
Intent i = new Intent(this, Situation1.class);
i.putExtras(b);
startActivity(i);

While getting (most probably in oncreate of Situation1 activity):

Bundle b=this.getIntent().getExtras();
int[] array=b.getIntArray("arr");
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64