Diptangsu Goswami
Diptangsu Goswami

Reputation: 5965

How to add/pass multiple values to Intent object?

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.

Upvotes: 2

Views: 2831

Answers (3)

borrom
borrom

Reputation: 551

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

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13348

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

Upvotes: 0

Let'sRefactor
Let'sRefactor

Reputation: 3346

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.

Upvotes: 1

Related Questions