Android onActivityResult is always 0
Asked Answered
S

3

18

This has been killing me for two days now. I have a main Activity A which calls a second Activity B. Activity B simply presents the user with a listview. When I press an item on the list view I want a couple of strings to be passed back to the main Activity A and Activiy B will finish.

The problem is I always get a resultcode of 0 and the data bundle is null. I really don't understand why this is happening.

Here is my code.

Start Activity B for result:

Test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(recipeActivity.this, BrowseLoadRecipes.class);
            startActivityForResult(i, RECIPE_CHOOSER);
    }  
    });

This starts the second Activity fine. Activity B populates a listview and when I click an item I'm trying to send some data back to the calling Activity A.

Any text at the moment, so I used the following in Activity B:

     lv.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString("TEXT", "Please work... pleeeeaasee");
        Intent mIntent = new Intent();
        mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
        finish();
     }
     });

In the calling activity I have the following listening for the return as follows:

protected void onActivityResult(int requestCode, int resultCode, 
        Intent data) { 
            switch(requestCode) { 
            //TODO
            case RECIPE_CHOOSER:
                Toast.makeText(getApplicationContext(), "In recipe return", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "resultCode is " + String.valueOf(resultCode), Toast.LENGTH_SHORT).show();
                if (resultCode == RESULT_OK) {
                Bundle b = getIntent().getExtras();

                Toast.makeText(getApplicationContext(), "Returned " + b.getString("TEXT"), Toast.LENGTH_LONG).show();
                }
                if (resultCode == RESULT_CANCELED) {

                    }
                break;

                } 
            } 
        }

I can see that the request code is correctly returned, but the resultcode is always a 0 and the data is always a null.

I've ran through the debug and the setResult is doing its job and the bundle does indeed have the data I'm passing, but it's lost at some point along the way.

Is there something in the manifest I'm missing or something. It's killed my progress on this project so far.

Sciamachy answered 22/12, 2010 at 14:40 Comment(0)
C
15

In your list activity, onItemClickListener try the following replacing the setResult lines with:

if (getParent() == null) {
    setResult(Activity.RESULT_OK, data);
}
else {
    getParent().setResult(Activity.RESULT_OK, data);
}

I'm wondering whether there is a parent activity which is what you need to bind the data to and set the result on....

Corrincorrina answered 22/12, 2010 at 14:49 Comment(5)
I think this is going to work. I'll try it now and get back to you in five minutes. Thanks for the reply.Sciamachy
Dave... if I were there with you right now I'd buy you lovely cold pint. It worked perfectly. Thanks again...Sciamachy
After thinking about this I now know the reason why my original code wasn't working. The intent I fire up first (Activity B) is really just a front tab holder intent which then starts two more intents on two different tabs (first tab to view local files, the second to view web located files). The main returning intent should have been the very first intent, hence why Daves excellent answer points me back to the Parent. I lived and leared today.Sciamachy
The tab holder got me exactly the same couple of weeks ago too... Glad your sorted now, you can have a nice christmas :-)Corrincorrina
this answer saved my day ... +1 :)Dichotomy
T
5

Concerning your returned data.

You do:

Bundle b = getIntent().getExtras();

but "getIntent()" returns the Intent that started THIS activity. If you want the returned data from the Activity you started for result, just take the data which is passed to

protected void onActivityResult(int requestCode, int resultCode, Intent data)
Thereabout answered 22/12, 2010 at 14:53 Comment(0)
R
-3

Your code is perfectly working........

in u Activity B
use
  setResult(0, mIntent);insted of setResult(RESULT_OK, mIntent);
in your Activity A:
use
case 0: insted case RECIPE_CHOOSER: and 
use System.out.println(b.getString("TEXT"));

You will get output as

Please work... pleeeeaasee

Revell answered 6/12, 2012 at 9:51 Comment(1)
That is masking the issue, really. 0 is the value for the RESULT_CANCELED enumeration, not the RESULT_OK one. And any return from Activity B (when calling finish()) that doesn't have a result code set is automatically set to RESULT_CANCELEDLint

© 2022 - 2024 — McMap. All rights reserved.