marchemike
marchemike

Reputation: 3277

clarification on how send data to previous Activity using startActivityForResult

I am currently trying to send data from a child activity back to its parent activity. I have used the startActivityForResult() to go to my child activity. However I cannot understand what I would do to put the data I want to put back to my intent and make my parent activity receive it. I have been looking at different examples online but I think it is only throwing the result variable.

As per my understanding this is what I used when I return my child activity back to the parent activity:

         String somestring = "somevalue";
         Intent i = getIntent();

         setResult(RESULT_OK, i);
         finish(); 

I want to load the content of the string somestring for it to return back to the parent activity. How can I load it back to my parent activity?

            startActivityForResult(intent, 1);

And lastly how can I capture the data in my parent activity in the onActivityResult?

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);


}

Upvotes: 1

Views: 140

Answers (1)

Karl
Karl

Reputation: 3464

Add your data to an intent, just as you would in any other situation:

Intent intent=new Intent();
intent.putExtra("ComingFrom", "Hello");
setResult(RESULT_OK, intent);
finish();

Then retrieve it in onActivityResult in your other activity.

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data)
{
     super.onActivityResult(requestCode, resultCode, data);
     String extraData=data.getStringExtra("ComingFrom"));
}

Upvotes: 4

Related Questions