How to use putExtra() and getExtra() for string data
Use this to "put" the file...
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String strName = null;
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
first Screen.java
text=(TextView)findViewById(R.id.tv1);
edit=(EditText)findViewById(R.id.edit);
button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String s=edit.getText().toString();
Intent ii=new Intent(MainActivity.this, newclass.class);
ii.putExtra("name", s);
startActivity(ii);
}
});
Second Screen.java
public class newclass extends Activity
{
private TextView Textv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
Textv = (TextView)findViewById(R.id.tv2);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String j =(String) b.get("name");
Textv.setText(j);
}
}
}
Best Method...
SendingActivity
Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent.putExtra("keyName", value); // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
RecievingActivity
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName"); // retrieve the data using keyName
/// shortest way to recieve data..
String data = getIntent().getExtras().getString("keyName","defaultKey");
//This requires api 12.
//the second parameter is optional . If keyName is null then use the defaultkey
as data.