-2

Possible Duplicate:
Passing data between activities in Android

I'm trying to pass a string from my main activity to another activity I have created. How would I do this?

Community
  • 1
  • 1
Miguel
  • 1,157
  • 1
  • 15
  • 28

3 Answers3

1

You would want to save your information into a bundle as an extras. Please see the code below

This would be for your First activity

String customerName = "Bob";
Bundle b = new Bundle();
Intent myIntent = new Intent(v.getContext(),work.class);

b.putString("Name", customerName);  
myIntent.putExtras(b);
v.getContext().startActivity(myIntent);

Then to access the information in the other(Second) activity please see the code below

Bundle b = getIntent().getExtras(); 
String name = b.getString("customerName");      
Miguel
  • 1,157
  • 1
  • 15
  • 28
0

try this

FirstActivity

Bundle bundle = new Bundle();
bundle.putString("Your_KEY", "YOUR_STRING_VALUE");

Intent newIntent = new Intent(FirstActivity.this.getApplicationContext(), SecondActivity.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

SecondActivity

Bundle bundle = SecondActivity.this.getIntent().getExtras();
String s = bundle.getString("Your_KEY");
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
0

In the secondActivity

public static String myName;

myName = "Miguel";

When you want to get it in the mainActivity:

 String s1;

 s1= secondActivity.myName;
Hossam Alaa
  • 663
  • 5
  • 9