onActivityResult method is not being called

流过昼夜 提交于 2020-02-02 01:02:30

问题


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


    回答1:


    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.



    来源:https://stackoverflow.com/questions/20354701/onactivityresult-method-is-not-being-called

  • 易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!