0

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.

  • check this answer: – Vinay Jul 25 '19 at 07:44
  • 1
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Basi Jul 25 '19 at 08:05

5 Answers5

1

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

1

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)
Abu Yousuf
  • 4,421
  • 2
  • 23
  • 39
0

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

        }
    }
}
0

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)
Abu Yousuf
  • 4,421
  • 2
  • 23
  • 39
0

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

Basi
  • 2,675
  • 19
  • 28