onActivityResult method is not being called
Asked Answered
M

1

7

I'm having a problem in my android application. I don't know why 'onActivityResult' method is not being called when 'Up navigation' button from action bar is pressed. I think I've done everything properly:

  • Parent activity launch child activity with 'startActivityForResult' method.
    Intent intent = new Intent(ParentActivity.this, ChildActivity.class);
    startActivityForResult(intent, 1000);
    

  • Parent activity has overridden 'onActivityResult' method.
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
       super.onActivityResult(requestCode, resultCode, data);   
       if (data != null && requestCode == 1000)
       {
            Bundle extras = data.getExtras();
            Boolean rc = extras.getBoolean(MyConstants.INTENT_EXTRA_RESULT);
            if (rc)
            {
                .......
            }
        }
    }
    

  • Child activity has overridden 'onOptionsItemSelected' and calls 'NavUtils.navigateUpFromSameTask'.
    public boolean onOptionsItemSelected(MenuItem item)
      {
        if (item.getItemId() == android.R.id.home)
        {
            Intent result = new Intent((String)null);
            result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
            setResult(RESULT_OK, result);
            NavUtils.navigateUpFromSameTask(this);          
            return true;
        }           
        else
        {
            return super.onOptionsItemSelected(item);
        }
    }
    

  • Child activity has overriden 'finish' method. This method set a result.
    public void finish() 
    {
       Intent result = new Intent((String)null);
       result.putExtra(Constantes.INTENT_EXTRA_HAY_QUE_RECALCULAR, hayQueRecalcular);               
       setResult(RESULT_OK, result);
    
       super.finish();
    }   
    

    I'm not sure why 'onActivityResult' method is not being called.

    What I've observed is that child activity is not being finished ('finish' method is not being called) when 'Up navigation' button from action bar is pressed. However it is called when back button (hardware button) is pressed.

    What I'm doing wrong?

    Thanks

  • Metamorphose answered 3/12, 2013 at 15:8 Comment(6)
    what is your request code that you are giving while using startActivityForResult(Intent,int)? is it a positive integer?Teratoid
    Check the launch mode of you parent activity. See here - https://mcmap.net/q/206505/-android-onactivityresult-not-called-triggeredHardie
    Yes, it is a positive integer (1000).Metamorphose
    I haven't defined launch mode of my parent activityMetamorphose
    Please post your code which starts the activity, and handles the activity resultSemibreve
    I've posted the code you asked me.Metamorphose
    T
    5

    As your child Activity is just on the top of your Parent Activity, no need of this method

     NavUtils.navigateUpFromSameTask(this);    
    

    write like

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            Intent result = new Intent((String) null);
            result.putExtra(MyConstants.INTENT_EXTRA_RESULT, true);
            setResult(RESULT_OK, result);
            finish();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
    

    finish your Child Activity when home button is pressed.

    Teratoid answered 3/12, 2013 at 15:35 Comment(4)
    Thanks for your help. Then I don't understand what 'navigateUpFromSameTask method' is for. I thought it was to navigate to the parent activity. Android documentation says: "To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity."Metamorphose
    Thanks, now works but I still doesn't understand why 'NavUtils.navigateUpFromSameTask(this)' doesn't finish the child activity.Metamorphose
    @Metamorphose Don't know exactly about behaviour of NavUtils. I think it directly removes Activity from task.Teratoid
    what if u have a second child activity in between? how do you send the result to the parent?Mongolism

    © 2022 - 2024 — McMap. All rights reserved.