By Hesam

2012-05-02 03:03:00 8 Comments

In my activity, I'm calling a second activity from the main activity by startActivityForResult. In my second activity there are some methods that finish this activity (maybe without result), however, just one of them return a result.

For example, from the main activity I call a second one. In this activity I'm checking some features of handset such as does it have a camera. If it doesn't have then I'll close this activity. Also, during preparation of MediaRecorder or MediaPlayer if a problem happens then I'll close this activity.

If its device has a camera and recording is done completely, then after recording a video if a user clicks on the done button then I'll send the result (address of the recorded video) back to main activity.

How do I check the result from the main activity?


@Nitesh Dev Kunwar 2019-04-24 13:38:57

You need to override Activity.onActivityResult()

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

if (resultCode == RESULT_CODE_ONE) {

   String a = data.getStringExtra("RESULT_CODE_ONE");

else if(resultCode == RESULT_CODE_TWO){

   // b was clicked



@Nishant 2012-05-02 03:36:38

From your FirstActivity call the SecondActivity using startActivityForResult() method

For example:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.

For example: In SecondActivity if you want to send back data:

Intent returnIntent = new Intent();

If you don't want to return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);

Now in your FirstActivity class write following code for the onActivityResult() method.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result

@Ismail Sahin 2013-10-30 23:05:45

What is the purpose of putting an intent when RESUT_CANCELLED in setResult(RESULT_CANCELED, returnIntent);

@Nishant 2013-10-31 04:42:00

@ismail Suppose in the SecondActivity some exception occurred, in that case also you need to return the result to the FirstActivity,so you can set the result as "RESULT_CANCELLED" in the catch block and return to FirstActivty and in FirstActivity's' 'onActivityResult() you can check whether you got the success or failure result.

@Ismail Sahin 2013-10-31 10:43:10

So it's up to you, if you don't need to know the reason for cancelling, you can use just setResult(RESULT_CANCELED); without any intent

@Francisco Corrales Morales 2014-05-09 14:23:53

can I do this in an AsynkTask as the SecondActivity?,

@Nishant 2014-05-17 19:49:44

@FranciscoCorrales If you have to start SecondActivity from AsyncTask, Yes you can do this.

@Shafi 2014-09-15 09:45:57

@Nishant How to implement the same thing in fragments instead of activity?

@Nishant 2014-09-20 16:43:00

@Shafi there is no concept of onActivityResult() call back for fragments as fragments basically are the part of a single Activity.If you want to communicate between two fragments you can see this link…

@Lei Leyba 2014-09-30 05:49:35

finish isn't called after startActivityForResult?

@Nishant 2014-10-01 06:26:59

@Lei Leyba No finish() is not called after calling startActivityForResult().First Actvity will move to pause state.

@Martin Pfeffer 2015-03-09 21:51:56

For me it is not working -.- this is what I hate soooo much about Android - this system is so unreliable :-/

@Ruslan Berozov 2017-06-01 15:06:52

What happens if we have three or more actvities? I have a problem with it now. I need to return to first activity from third through second, but I loss data in onActivityResult(...) in first activity after this and resultCode = CANCELED...

@spydon 2017-08-07 23:03:14

You can use setResult(RESULT_OK); for example directly before calling finish(); without creating a new intent in newer versions of android.

@Damir Varevac 2018-07-08 08:58:32

Works perfectly for some simple use cases.

@Harsha Vardhan 2019-07-19 08:54:47

How come setResult method is not mentioned in the guide I'm curious to know how you've learned it.

@Rohit Singh 2017-12-22 09:54:47

Very common problem in android
It can be broken down into 3 Pieces
1 ) start Activity B (Happens in Activity A)
2 ) Set requested data (Happens in activity B)
3 ) Receive requested data (Happens in activity A)

1) startActivity B

Intent i = new Intent(A.this, B.class);

2) Set requested data

In this part, you decide whether you want to send data back or not when a particular event occurs.
Eg: In activity B there is an EditText and two buttons b1, b2.
Clicking on Button b1 sends data back to activity A
Clicking on Button b2 does not send any data.

Sending data

   Intent resultIntent = new Intent();

Not sending data

       setResult(RES_CODE_B,new Intent());

user clicks back button
By default, the result is set with Activity.RESULT_CANCEL response code

3) Retrieve result

For that override onActivityResult method

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

if (resultCode == RES_CODE_A) {

     // b1 was clicked 
   String x = data.getStringExtra("RES_CODE_A");

else if(resultCode == RES_CODE_B){

   // b2 was clicked

   // back button clicked 

@Suragch 2016-12-05 08:36:32


To see the entire process in context, here is a supplemental answer. See my fuller answer for more explanation.

enter image description here

public class MainActivity extends AppCompatActivity {

    // Add a different request code for every activity you are starting from here
    private static final int SECOND_ACTIVITY_REQUEST_CODE = 0;

    protected void onCreate(Bundle savedInstanceState) {

    // "Go to Second Activity" button click
    public void onButtonClick(View view) {

        // Start the SecondActivity
        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);

    // This method is called when the second activity finishes
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // check that it is the SecondActivity with an OK result
        if (requestCode == SECOND_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) { // Activity.RESULT_OK

                // get String data from Intent
                String returnString = data.getStringExtra("keyName");

                // set text view with string
                TextView textView = (TextView) findViewById(;

public class SecondActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

    // "Send text back" button click
    public void onButtonClick(View view) {

        // get the text from the EditText
        EditText editText = (EditText) findViewById(;
        String stringToPassBack = editText.getText().toString();

        // put the String to pass back into an Intent and close this activity
        Intent intent = new Intent();
        intent.putExtra("keyName", stringToPassBack);
        setResult(RESULT_OK, intent);

@Jerry Abraham 2018-10-25 10:21:13

Can this done by two different apps A and app b?…

@Tomasz Mularczyk 2015-10-23 12:58:55

For those who have problem with wrong requestCode in onActivityResult

If you are calling startActivityForResult() from your Fragment, the requestCode is changed by the Activity that owns the Fragment.

If you want to get the correct resultCode in your activity try this:


startActivityForResult(intent, 1); To:

getActivity().startActivityForResult(intent, 1);

@Julian Alberto 2015-05-26 20:40:47

Complementing the answer from @Nishant,the best way to return the activity result is:

Intent returnIntent = getIntent();

I was having problem with

new Intent();

Then I found out that the correct way is using


to get the current intent

@spaaarky21 2018-06-29 16:52:54

It feels a bit odd to create a new Intent that only exists to hold a Bundle and doesn't have the normal values like an action or component. But it also feels a bit odd (and potentially dangerous?) to modify the Intent that was used to launch the current activity. So I searched the source for Android itself and found that they always create a new Intent to use as the result. For example,…

@Julian Alberto 2018-06-29 18:29:55

Hello spaaarky21, thank you for your comment. I'm sorry I wasn't so clear in explaining exactly how I ended up with that solution. It was three years ago, and I can only remember that my app was crashing because of "new Intent", that's what I meant when I said "I was having problem with". Actually I just tryed with "getIntent", because it made sense at the time, and it worked! Because of it I decided to share my solution. Maybe not the best choise of words to say "best way" or "correct way", but I stand by my solution. It's what solved my problem and aparently of other people too. Thanks

@Julian Alberto 2018-06-29 18:32:32

And I'm not actually modifying the Intent that was used to launch the current activity, I'm just adding an extra called "result"

@sam 2019-02-25 11:37:26

Wow! works great. getIntent() seem to be a perfect way to return data to unkown activity, from where the activity has been called. Thanks!

@Dharmendra Pratap 2014-09-30 12:30:37

First you use startActivityForResult() with parameters in first Activity and if you want to send data from second Activity to first Activity then pass value using Intent with setResult() method and get that data inside onActivityResult() method in first Activity.

@Sam 2012-05-02 03:07:14

How to check the result from the main activity?

You need to override Activity.onActivityResult() then check its parameters:

  • requestCode identifies which app returned these results. This is defined by you when you call startActivityForResult().
  • resultCode informs you whether this app succeeded, failed, or something different
  • data holds any information returned by this app. This may be null.

@JCarlos 2017-08-11 23:32:13

It means that the requestCode is only used in the first activity, and it is never used for the 2nd activity? If the 2nd activity has different approaches, it would change, but based in the intent extras and not by the requestCode, right? Edit: Yes,…

@DaviF 2014-03-08 13:54:56

If you want to update the user interface with activity result, you can't to use this.runOnUiThread(new Runnable() {} Doing this the UI won't refresh with new value. Instead, you can do this:

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

    if (resultCode == RESULT_CANCELED) {

    global_lat = data.getDoubleExtra("LATITUDE", 0);
    global_lng = data.getDoubleExtra("LONGITUDE", 0);
    new_latlng = true;

protected void onResume() {

        PhysicalTagProperties.this.setLocation(global_lat, global_lng);

This seems silly but works pretty well.

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] Is there a unique Android device ID?

94 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

35 Answered Questions

52 Answered Questions

27 Answered Questions

31 Answered Questions

[SOLVED] Activity restart on rotation Android

77 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

2 Answered Questions

How do i manage startActivityForResult on Android?

  • 2019-01-29 17:25:25
  • Raj
  • 46 View
  • 0 Score
  • 2 Answer
  • Tags:   android

Sponsored Content