1

I have two activities. The second activity is called from the first using startActivityForResult(intent,request_code). In the second activity I have this code:

Intent i = getIntent();
i.putExtra("data" , some data);
setResult(Activity.RESULT_OK,i);
finish();

Then, to get the data in the first activity I use this code:

    @Override
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==this.uploadRequestCode && resultCode == Activity.RESULT_OK)
    {
        data = intent.get("data")
    }

I start the activity this way:

Intent uploadIntent = new Intent(this,uploadActivity.class)
startActivityForResult(uploadIntent,this.uploadRequestCode)

The problem is that the result code I get is RESULT_CANCELLED even though I set RESULT_OK.

NOTE

I am not talking about a situation when back button is pressed.

UPDATE

I found out that the intent I get in onActivityResult() is null even though I sent an intent, that's why the result code was RESULT_CANCELLED.

Anyone knows why the intent is null?

2 Answers2

3

Maybe this can help you out, something from a project that I have

This how I do start my activity

Intent intent = new Intent(FirstActivity.this, ActivityThatReturnInfo.class);
startActivityForResult(intent, 1);

This is from the activity that I want to return some value

Intent returnIntent = new Intent();
returnIntent.putExtra("VAR",someInfo);
setResult(Activity.RESULT_OK,returnIntent);
finish();

This is on the activity that as started the one that is waiting for the information

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case 1:
            if(resultCode == Activity.RESULT_OK)
            {
                String result = data.getStringExtra("VAR");
                // Code to do
            }
            break;
        default:
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null)
            {
                if(result.getContents() != null)
                {
                    // Code to do
                }
            }
            break;
    }
}
Camadas
  • 509
  • 1
  • 5
  • 21
  • thanks, but that's not answering my question. My problem was that the result code is CANCELLED even though I sent OK. Means, it doesn't get inside the if condition – Gili Jacobi Mar 28 '18 at 11:27
  • Ohh sry, try to do what ss_ told on is anwser, replace the `setResult(Activity.RESULT_OK,i);` with `setResult(RESULT_OK,i);` if that doesn't work, I don't know the default value for the `Activity.RESULT_OK` but just in case do a log of the `resultCode` to see if comes as correct – Camadas Mar 28 '18 at 11:32
  • I tried sending RESULT_OK, Activity.RESULT_OK and -1 which is the value of RESULT_OK. Still doesn't work – Gili Jacobi Mar 28 '18 at 11:40
  • Strange, did you checked the `requestCode` if its the one that `this.uploadRequestCode` has? Since both must be true to enter in the `if` maybe the problem comes from it. – Camadas Mar 28 '18 at 11:43
  • yes I checked both values and the problem is with the result code – Gili Jacobi Mar 28 '18 at 11:46
  • I know that sounds silly, but if you all rdy have done `setResult(-1,intent);` and the default value of `Activity.RESULT_OK` is -1 then the `Activity.RESULT_OK` can't be -1 also there, on the if try to put like this `if (requestCode== 100 && resultCode == 1)` start the activity as `startActivityForResult(i, 100);` and before finish change to `setResult(1,i);` – Camadas Mar 28 '18 at 12:06
  • RESULT_OK is really -1 but I got RESULT_CANCELLED because the intent passed was null. Do you know what could have caused this problem? – Gili Jacobi Mar 28 '18 at 12:47
  • From what I have understood what is null is this `this.uploadRequestCode` ? if yes, try put the raw value of `request_code` in `startActivityForResult(intent,request_code)`, put the value not the variable to see if still comes null or not – Camadas Mar 28 '18 at 13:06
  • that's not the problem, `this.uploadRequestCode` is not null. However, the intent I sent back is null at onActivtyResult() even though I did sent an intent – Gili Jacobi Mar 28 '18 at 13:58
  • could u update how to you start the activity? ex `Intent intent = new Intent(FirstActivity.this, ActivityThatReturnInfo.class);` ? Do you start any other activity after that ? Rly don't understand why its null, if the `Intent i = new ......` is created properly it should not be empty – Camadas Mar 28 '18 at 14:27
  • I start a camera intent in the middle of the second activity. The ending of the second activity (going back to the first activity) happens inside the `onActivityResult` of the camera intent. – Gili Jacobi Mar 28 '18 at 15:24
  • Just for the sake of debug, could you not start the camera intent, put something in the variable and see how it does behave ? – Camadas Mar 28 '18 at 15:36
  • whic variable are you talking about? – Gili Jacobi Mar 28 '18 at 17:17
  • The one that you are waiting at the `onActivityResult` – Camadas Mar 28 '18 at 17:35
  • `setResult(Activity.RESULT_OK,returnIntent);` saved my day thanks. – Shailendra Madda Apr 30 '21 at 12:38
2

Starting SecondActivity from FirstActivity

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

Sending data to FirstActivity from SecondActivity

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

getting result in FirstActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == 100 && resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
            Log.e("Result",result);
    }
}

Edit: Change your code to below and try once. While sending intent from SecondActivity to FirstActivity Intent i = getIntent(); to Intent intent= new Intent();

Sabyasachi
  • 3,499
  • 2
  • 14
  • 21