Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return string from activity started for result

Tags:

android

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

like image 718
Damir Avatar asked Feb 10 '12 09:02

Damir


People also ask

What are the return types when returning an activity with result?

What are return types of startActivityForResult in android Options 1 RESULT OK 2 RESULT CANCEL 3 RESULT CRASH 4 A.

Which method is used to start an activity which will return a value to its caller?

In the startActivityForResult() method call you can specify a result code to determine which activity you started. This result code is returned to you. The started activity can also set a result code which the caller can use to determine if the activity was canceled or not.

Is start activity for result deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


1 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");
      }
   }
}
like image 137
jeet Avatar answered Nov 15 '22 22:11

jeet