onActivityResult is not called when the back button in ActionBar is clicked

大憨熊 提交于 2019-12-12 12:17:08

问题


Here is my problem:

  1. Create a MainActivity. Add a button which will start another activity SecondActivity.

            Intent i = new Intent(getActivity(),SecondActivity.class);
            startActivityForResult(i,0);
    
  2. Inside the SecondActivity, I capture the back button click event and also add a button to return to the first Activity.

    When back button in action bar is clicked:

@Override

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            // back button
            Intent resultIntent = new Intent();
            // TODO Add extras or a data URI to this intent as appropriate.
            setResult(Activity.RESULT_OK, resultIntent);
            //finish();
            return false;

    }
    return super.onOptionsItemSelected(item);
}

When the button inside activity is clicked:

Button btn = (Button)this.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent resultIntent = new Intent();
        // TODO Add extras or a data URI to this intent as appropriate.
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }
});

The onActivityResult in MainActivity is called when I click the button inside the SecondActivity, but it's never been called if I click the back button in Actionbar of SecondActivity. Can anybody tell me why? Thanks


回答1:


Here is the code which is working:

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            // back button
            Intent resultIntent = new Intent();
            setResult(Activity.RESULT_OK, resultIntent);
            finish();
            return true;

    }
    return super.onOptionsItemSelected(item);
}

I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)




回答2:


Try this:-

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        Intent resultIntent = new Intent();
        setResult(Activity.RESULT_OK, resultIntent);
        onBackPressed();
        return true;
}
return super.onOptionsItemSelected(item);
}



回答3:


Good answer is Gopal Rao code in the same question. Its worked for me. This is a copy of his solution:

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


来源:https://stackoverflow.com/questions/20161234/onactivityresult-is-not-called-when-the-back-button-in-actionbar-is-clicked

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