3

Possible Duplicate:
How do I pass data between activities in Android?

I have two activities A and B. Activity A has one editText and a button.Activity B has a editText, when I type something in editText of A and click the button it should get displayed in editText2. Can anyone provide code for this.

Thanks in advance !!!

Community
  • 1
  • 1
Swetha Kaulwar
  • 111
  • 2
  • 3
  • 4
  • 1
    This is a basic Android question that has been answered many times in Stackoverflow: e.g. http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android, http://stackoverflow.com/questions/8256950/transfer-data-between-activity-a-to-c – azgolfer Jul 03 '12 at 03:06

4 Answers4

17

I will assume that you have written two Activity classes: ActivityA & ActivityB and that you have written the onClickListener for the button in ActivityA.

To pass data between two activities, you will need to use the Intent class via which you are starting the Activity and just before startActivity for ActivityB, you can populate it with data via the Extra objects. In your case, it will be the content of the editText.

ActivityA onClickListener

Intent i = new Intent(getBaseContext(),ActivityB.class);

//Set the Data to pass
EditText txtInput = (EditText)findViewById(R.id.txtInput);
String txtData = txtInput.getText().toString();
i.putExtra("txtData", txtData);

startActivity(i);

Now in ActivityB, you can write code in the onCreate to get the Intent that launched it and extract out the data passed to it.

ActivityB onCreate

Intent i = getIntent();
//The second parameter below is the default string returned if the value is not there. 
String txtData = i.getExtras().getString("txtData","");
EditText txtInput2 = (EditText)findViewById(R.id.txtInput2);
txtInput2.setText(txtData);

Hope this helps.

Romin
  • 8,708
  • 2
  • 24
  • 28
5

When you're starting activity B send data in intent extras.

In Activity A, When you're starting activity B,

Intent activityBstartIntent = new Intent(getApplicationContext(), ActivityB.class);
activityBstartIntent.putExtra("key", editTextA.getText().toString());
startActivity(activityBstartIntent);

And in onCreate() of ActivityB do this

if(getIntent().getExtras() != null) {
editTextB.setText(getIntent().getExtras().getString("key");
}

Hope that helps.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
3

By using intent we can pass data across components like activities. In your first activity on click on button you need to write this get send data to second activity.

Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);

EXTRA_MESSAGE is String constant in your activity.

And second activity you will get that message like this

Intent intent = getIntent();

String message = intent.getStringExtra(MyFirstActivity.EXTRA_MESSAGE);

Here it's explained very clearly.

Sankar
  • 1,685
  • 2
  • 16
  • 27
0

Yes, someone can provide code for this, and Google even made it the subject of many tutorials and sample code. Try reading about Intents.

Geobits
  • 22,218
  • 6
  • 59
  • 103