Hi Friends,
i am using two Activity as Old Activity and New Activity,i want to pass some value from New Activity to Old Activity.how can i pass the value from New Activity to Old Activity please give some example code for that.... Thanks All
Hi Friends,
i am using two Activity as Old Activity and New Activity,i want to pass some value from New Activity to Old Activity.how can i pass the value from New Activity to Old Activity please give some example code for that.... Thanks All
an example::
Bundle bundle = new Bundle();
bundle.putInt("newPicPosition", position);
Intent intent = new Intent(NewActivity.this, OldActivity.class);
intent.putExtras(bundle);
setResult(Activity.RESULT_OK, intent);
finish();
use setResult()
public final void setResult (int resultCode, Intent data) Since: API Level 1 Call this to set the result that your activity will return to its caller.
Parameters::
resultCode The result code to propagate back to the originating activity, often RESULT_CANCELED or RESULT_OK
data The data to propagate back to the originating activity.
thanks Josrgesy, But i have one doubt,i can't get value from new activity,i am using Bundle b = getIntent().getExtras(); String s=b.getString("name"); Log.v("name",s); how can i get value? – sivaraj
private String s;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (0) : {
if (resultCode == Activity.RESULT_OK) {
if (data != null){
Bundle b= data.getExtras();
s= b.getString("name");
Log.v("name",s);
}
break;
}
}
}
}
Update::
from your old activity you must start your newactivity with RequestCode = 0
Bundle bundle = new Bundle();
bundle.putString("foo",foovalue);
Intent newIntent = new Intent(OldActivity.this, newActivity.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
very important startActivityForResult(newIntent, 0);