thejaswi
thejaswi

Reputation: 175

Receive the response from second activity

I am beginner in android, I just want to know how to get the response from the second activity to which I came from first activity.

Can any body tell me the way to do this? Is it before while calling finish() ?

I would be thankful if any one can show me the code snippet.

Upvotes: 2

Views: 5527

Answers (2)

Sandeep Kumar P K
Sandeep Kumar P K

Reputation: 7472

Call second activity like this

 Intent myIntent = new Intent(this,SecondActivity.class);

 startActivityForResult(myIntent, 37);

Finish SecondActivity like this

Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();


Override onActivityResult in FirstActivity like this

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

        if (requestCode == 37) {
            if (resultCode == Activity.RESULT_OK) {                


            }
        }


   }

Upvotes: 9

Cristian
Cristian

Reputation: 200080

You must call the second activity using the startActivityForResult method. In your second activity, when it is finished, you can execute the setResult method where basically you put the result information. Then, on your first activity, you override the onActivityResult method.

Upvotes: 5

Related Questions