views:

242

answers:

2

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

+1  A: 

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.

Jorgesys
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
ok I have posted how you can get the values from new activity =)
Jorgesys
A: 

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);

Jorgesys
@Thanks Jorgesys,i used this code in old Activity,i got null pointer exception in old Activity.what can i do,i am fresher in android
sivaraj
are you sending the value of "name" from new Activity ??? Bundle bundle = new Bundle();bundle.putString("name", name_vale); Intent intent = new Intent(NewActivity.this, OldActivity.class);intent.putExtras(bundle);setResult(Activity.RESULT_OK, intent);
Jorgesys
@Hi Jorgesys now i rectify null pointer exception,but i did not get value from s.i pass some value in name ...how can i print the result in old activity
sivaraj
check in NewActivity if name_value is null before send to old activity, you can print with Log.v("*** value of name is ",name_value);
Jorgesys
@Jorgesys Bundle bundle = new Bundle(); bundle.putString("name", name_vale); Intent intent = new Intent(NewActivity.this, OldActivity.class); intent.putExtras(bundle); setResult(Activity.RESULT_OK, intent);i write this code in bt_back click listener
sivaraj
@Jorgesys i passed value like bundle.putString("name", "position"); in newActivity
sivaraj
hi sivaraj check my update.
Jorgesys
@hi Jorgesys i got result thank you very much for your helps
sivaraj
youre welcome! =)
Jorgesys
@Jorgesys now i am passing string array for name variable in new Activity ,but now i got null pointer exception in old Activity what problem.. i have checked name variable and print value of name.value printed.how to solve this problem thanks..Jorgesys
sivaraj
are u using .putStringArray() to send and .getStringArray() to receive the value???
Jorgesys
@jorgesys i am using putStringArray() to send and getSttringArray() to receive the value but i got null pointer exception in NewActivityplease give any salutation for it....Thanks jorgesys
sivaraj
@Today i have used yesterday code in new activity and oldactivity but i didn't get any exception from there but i did not get result therewhy?what problem there..i got it like ..INFO/ActivityManager(82): Starting activity: Intent { cmp=com.fsp.blacksheep/.oldActivity(has extras) }
sivaraj
@i got result thanks
sivaraj