Home » Android » android – How to return string from activity started for result

android – How to return string from activity started for result

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have started Activity for result, but how to return string like parameter from that activity ?

How to&Answers:

just use following code block:

Intent intent=new Intent();
intent.putExtra("RESULT_STRING", string);
setResult(RESULT_OK, intent);
finish();

get value from this intent in onActivtyResult method in calling activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode == CREATE_REQUEST_CODE) {
      if (resultCode == RESULT_OK) {
        //Use Data to get string
        String string = data.getStringExtra("RESULT_STRING");
      }
   }
}

Answer:

You just need to putExtra in the intent and the call setResult(),

Intent data = new Intent();
data.putExtra("myobj", value);
setResult(Activity.RESULT_OK, data);

Answer:

The documentation says it all. You set the result by calling setResult and you read it in the onActivityResult method.