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

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179

3 Answers3

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

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • 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. – Bagusflyer Nov 23 '13 at 10:57
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);
}
Hardip Patel
  • 85
  • 2
  • 11
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);
       }
    }
Community
  • 1
  • 1
Al Dante
  • 23
  • 1
  • 6