Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

android - How to pass integer from one Activity to another?

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

57.0k users

...