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!

0

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

How do I do that? Thank you for the immediate response!
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.