Reputation: 3
Hello and sorry for my english i'm french. I'm learning Android development and i try to send int value in an other activity. I have declared an int variable to 0 and when i press a button the variable go to 1 on other value for each buttons. I know how to create an intent but how can i make it get the value of my button. Thanks.
Upvotes: 0
Views: 144
Reputation: 152
It's simple.
On the sending side
int intValue
= get value from edit text or button
use Intent.putExtra
to set value
Intent myIntent = new Intent(test1.this, test2.class);
myIntent.putExtra("yourname", intValue);
startActivity(myIntent);
On the receiver side
use Intent.getIntExtra
to get value
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("yourname", 0);
intValue
is your value
Upvotes: 1
Reputation: 66
You would need to use putExtra on your intent to add the int value you want to send to the next activity like so:
val intent = Intent(this, NextActivity::class.java)
intent.putExtra("samplevalue", 1)
startActivity(intent)
Then on that activity (NextActivity), you will use the code below to retrieve the value.
val buttonValue:Int = intent.getIntExtra("samplevalue", 0)
Upvotes: 1
Reputation: 3158
You can specify the parameters by putting key-value pairs into the intent bundle:
// ActivityOne.java
public void launch() {
// first parameter is the context, second is the class of the activity to launch
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
// put "extras" into the bundle for access in the second activity
i.putExtra("username", "foobar");
i.putExtra("in_reply_to", "george");
i.putExtra("code", 400);
// brings up the second activity
startActivity(i);
}
Once you have added data into the bundle, you can easily access that data within the launched activity:
// ActivityTwo.java (subactivity) can access any extras passed in
protected void onCreate(Bundle savedInstanceState) {
String username = getIntent().getStringExtra("username");
String inReplyTo = getIntent().getStringExtra("in_reply_to");
int code = getIntent().getIntExtra("code", 0);
}
more information check this
Upvotes: 0
Reputation: 6107
Create a Intent for your target Activity then use putExtra method to put data in Intent.
val intent = Intent(context, TargetActivity::class.java)
intent.putExtra("some_value", value)
Then start your Activity
startActivity(intent)
Upvotes: 0
Reputation: 583
first activity
Intent intent =new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("value", yourValue);
startActivity(intent);
second Activity
public class SecondActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
Intent iin = getIntent();
Bundle bundle = iin.getExtras();
if(bundle != null)
{
String name = (String) bundle.get("name");
}
}
}
Upvotes: 0