onActivityResult is not called when the back button in ActionBar is clicked
Asked Answered
T

3

4

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

Teat answered 23/11, 2013 at 10:32 Comment(3)
uncomment finish() in onOptionsItemSelected() and try once...Lupitalupo
I tried with finish() or without finish(). Both don't work.Teat
'return true;' is the real keyword for that issue. If you miss that,application doesn't close properly.Abstractionism
T
4

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

Teat answered 23/11, 2013 at 10:46 Comment(1)
The onCreate event in MainActivity will be triggered if I return false in onOptionsItemSelected. The MainActivity was recreated. I guess that is the reason why the onActivityResult is never been called.Teat
S
1

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);
}
Spadiceous answered 23/11, 2013 at 10:53 Comment(2)
I guess this is the same result with my own answer. Any difference between finish() and onBackPressed()?Teat
@bagusflyer It worked for me so thought may be on yours..but worth a try.Spadiceous
P
1

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);
       }
    }
Plymouth answered 11/4, 2014 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.