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.
putString("StringVariableName", intValue + ""); intent. putExtras(extras); startActivity(intent); The code above will pass your integer value as a string to class B. On class B, get the string value and convert again as an integer as shown below.
Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
//First Activity Intent i = new Intent(this, SecondActivity. class); i. putExtra("MY_KEY", 15); startActivity(i); //Second Activity int number = getIntent(). getExtras().
Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity . putExtra() adds extended data to the intent.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With