How to pass integer from one Activity to another?

benbeel picture benbeel · Aug 16, 2011 · Viewed 138.8k times · Source

I would like to pass a new value for an integer from one Activity to another. i.e.:

Activity B contains an

integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}

I would like activity A to pass a new value to activity B:

integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}

So that somehow through

private void startSwitcher() {
    Intent myIntent = new Intent(A.this, B.class);
    startActivity(myIntent);
}

I can set this integer value.

I know this can be done somehow with a bundle, but I am not sure how I could get these values passed from Activity A to Activity B.

Answer

Paresh Mayani picture Paresh Mayani · Aug 16, 2011

It's simple. On the sender side, use Intent.putExtra:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

On the receiver side, use Intent.getIntExtra:

 Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("intVariableName", 0);