2

i have two EditText objects in my first activity. i want both their values when i go to the next activity. Lets assume the EditText objects are inp1, inp2 and they can only accept numbers. please mention how i can add their values to int Intent object and how i will extract their values in my next activity's .java file.

Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36

3 Answers3

1

Here we go, your code will look like,

Sender Side:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName1", intValue1);
myIntent.putExtra("intVariableName2", intValue2);
startActivity(myIntent);

Receiver Side:

 Intent mIntent = getIntent();
 int intValue1 = mIntent.getIntExtra("intVariableName1", 0);
 int intValue2 = mIntent.getIntExtra("intVariableName2", 0);

Hope it helps.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
0

Use this code

Intent intent = new Intent(first.this, Second.class);
Bundle extras = new Bundle();
extras.putString("value1",String.valueof(inp1.getText().toString()));
extras.putString("value2",String.valueof(inp2.getText().toString()));
intent.putExtras(extras);
startActivity(intent);

Then in your second Activity onCreate()

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String value1 = extras.getString("value1");
String value2 = extras.getString("value2");
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

To make thing easier and reusable you can make your own intent like this

public class MyIntent extent Intent{
    private static final String FIRST_VALUE;
    private static final String SECOND_VALUE;

    public MyIntent(Context context, String firstValue, String secondValue){
        super(context,MySecondActivity.class);
        putExtra(FIRST_VALUE, firstValue);
        putExtra(SECOND_VALUE, secondValue);
    }

    public String getFirstValue(){
        getStringExtra(FIRST_VALUE);
    }

    public String getSecondValue(){
        getStringExtra(SECOND_VALUE);
    }
}

Sender:

startActivity(new MyIntent(this,"FirstString", "SecondString"));

Receiver Side:

MyIntent myIntent = (MyIntent)getIntent();
String firstValue  = myIntent.getFirstValue();
String secondValue = myIntent.getSecondValue();
Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
borrom
  • 441
  • 1
  • 5
  • 16