user2736219 user2736219 - 5 months ago 7
Android Question

Passing random generated integers to an intent

How to take a random generated integer and pass it to another Activity?

I have worked with Intents before, however since I never see the value of the integer, I cannot specify.

Basically,

int w = numGen.nextInt(7);


How do I put
w
into the Intent and use it in another Activity?

Answer
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("myrandom", yourRandomVal);
startActivity(this, intent);

In your other activity's onCreate():

int passedRandom = getIntent().getIntExtra("myrandom");

The thing is you can add extras to an intent by using various putExtra() and then get it using getExtra()