Bagusflyer
Bagusflyer

Reputation: 12925

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

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

Upvotes: 4

Views: 4526

Answers (3)

Al Dante
Al Dante

Reputation: 33

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);
       }
    }

Upvotes: 1

Hardip Patel
Hardip Patel

Reputation: 85

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);
}

Upvotes: 1

Bagusflyer
Bagusflyer

Reputation: 12925

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().)

Upvotes: 4

Related Questions