How do you pass a string from one activity to another? [duplicate]

Pass values using intents.

In your first activity

 Intent i= new Intent("com.example.secondActivity");
 i.putExtra("key",mystring);
 // for explicit intents 
 // Intent i= new Intent(ActivityName.this,SecondActivity.class);    
 // parameter 1 is the key
 // parameter 2 is the value 
 // your value
 startActivity(i);

In your second activity retrieve it.

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}

To pass custom objects you can have a look at this link

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/

Leave a Comment