Android Start Activity for Result returns different resultCode

There are a lot of questions out there on similar topics, but after searching around I haven't found one that matches my issue.

I know about starting intents for result, and overriding onActivityResult() to handle these intents, but for some reason, I'm having issues when I'm coming back from activity b to activity a. So, for example, in the last of 3 activities (AddDirections class) I start in my project, I call this method to return back to the previous activity:

public void finish(View v){
    Intent intent = new Intent(getApplicationContext(), AddIngredients.class);
    intent.putExtra(Home.RECIPE_INTENT, (Parcelable)recipe);
    intent.putExtra(Home.RECIPE_ID_INTENT, recipe.getId());
    setResult(RESULT_OK, intent);

In the AddIngredients class, I have this method:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == DIRECTIONS_REQUEST){
        Intent intent = new Intent(getApplicationContext(), NameRecipe.class);
        Recipe recipe = data.getParcelableExtra(Home.RECIPE_INTENT);
        intent.putExtra(Home.RECIPE_INTENT, (Parcelable)recipe);
        setResult(RESULT_OK, intent);

This should accept the returned result from the AddDirections class, and pass it off to NameRecipe, where I have the exact same method (Except for in the new Intent method it says Home.class). In the Home class, I have basically the same method again to receive the intent as it backs all the way out of the app.

Now, I will say that this works if I go straight through the steps from beginning to end. But if I use the up navigation to go the current activity's parent activity, then it messes everything up. Then when I click finish in the final step it messes up the resultCodes that I set for each intent. I make sure to explicitly set the correct result to RESULT_OK (which equals 1) but then for some reason, sometimes it changes what I've set to be the resultCode to be 0 instead.

Here's what I do in an activity if the user clicks the up navigation:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        Intent intent = NavUtils.getParentActivityIntent(this);
        intent.putExtra(Home.RECIPE_INTENT, (Parcelable)recipe);
        setResult(RESULT_OK, intent);
        NavUtils.navigateUpTo(this, intent);
        finish(); //Have tried with and without this
        return true;
    return super.onOptionsItemSelected(item);

Like I said, I've searched for a lot of reasons why the resultCode gets overwritten after using the up navigation, but I haven't found a single reason why.

Any help would be greatly appreciated.


It looks like the underlying question is really how do I navigate between multiple activities through the stack using the up navigation that is built in. I am wondering if the project is setup correctly for this type of navigation. From the android developer resources, up navigation from google, you have to setup the navigation buttons with a parent activity. Then the application can traverse between the different activities. From what it looks like this should allow A->B B->C c->D and back to A if needed. I would also add in a button that would allow a direct path back to A if the user so chooses.

If you have more information on what you are trying to accomplish between activities whether it is data passed back and forth or just returning to the previous activity that would be helpful. Hopefully the link will help your issue with this.

your problem seems to be the NavUp, this util class tends to clear everything to navigate to the parent including your resultCode and the instance of the activity, resulting in a Canceled result (if started for result) to a new instance of the parent activity, in order to fix that a quick fix would be to declare android:launchMode="singleTop" in your manifest, but I wouldn't recommend to use that unless you are 100% that you want that, instead I would go into my navigator class, or my method for navigate and overwrite that specific navigation to something like:

Intent parentActivityIntent = NavUtils.getParentActivityIntent(this); 
NavUtils.navigateUpTo(this, parentActivityIntent);

Hope this helps!! Sorry for the late answer :p

if by 'up navigation' you mean back key pressed then you need to override a function

public void onBackPressed() {
    // TODO Auto-generated method stub

if from what it seems to be you are trying to get back when clicking an item from the Options Menu to your parent activity directly then i think you will need to go back step by step as at this moment there is already an activity running and waiting for a result so when you go directly to home activity its already waiting an answer from activity B and not activity C i hope i explained it right :)

Need Your Help

Amending docstrings from decoators, while retaining formattting

python documentation decorator python-decorators

I have some decorator functions which execute additional code before/after executing the function they decorate. What I'd like to do is dynamically document the additional behavior to the decorated