0

I have an activity A that calls an activity B (FragmentActivity) using startActivityForResult(). Before activity B finishes by calling finish(), I use setResult(). In spite of all of this, onActivityResult() is not called. I'm not sure what I'm doing wrong as I'm not doing anything unconventional, or if this is an Android bug.

Seeing some of the suggestions on here, I've checked to see that I don't have android:launchMode="singleInstance" nor android:noHistory="true" set anywhere in my manifest file.

Calling activity B from A:

    Intent intent = new Intent(getActivity(), ActivityB.class);
    startActivityForResult(intent, MY_RESULT);

Finishing activity B:

    Intent intent = new Intent();
    setResult(Activity.RESULT_OK, intent);
    finish();

Overriding onActivityResult() in activity A:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        Toast.makeText(getApplication(), "toast", Toast.LENGTH_LONG).show();
    }

Edit 1

I commented out nearly all of activity B's code (see below), leaving A's intact and the problem remained. So, A is definitely doing something strange that causes the problem. Worse, I have done this with other activities (using the same code) without any problems! A extends ExpandableListActivity, although I don't understand how any of that code would cause issues in onActivityResult(). Logcat isn't giving me anything useful.

This is the code tested for activity B, for posterity's sake (though it seems to be that the problem is in A):

public class ActivityB extends FragmentActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_activityb);
        Intent intent = new Intent();
        intent.putExtra("data", "Hello from ActivityB");
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}

2 Answers2

0

try this and verify with your code:

ActivityA.java

public class ActivityA extends Activity{
   private static final int MY_RESULT = 0;
   private Button btnStart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        btnStart=(Button)findViewById(R.id.btnstart);
        btnStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          Intent intent = new Intent(this, ActivityB.class);
          startActivityForResult(intent, MY_RESULT);
        }
    });

  }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        Toast.makeText(getApplication(), data.getStringExtra("data"), Toast.LENGTH_LONG).show();
    }
}

ActivityB.java

public class ActivityB extends FragmentActivity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        Intent intent = new Intent();
        intent.putExtra("data", "Hello from ActivityB");
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

}

Note : make sure you have added both the activity in your manifest file.

Rustam
  • 6,485
  • 1
  • 25
  • 25
  • Thanks for the suggestion. I tried your code, commenting out pretty much everything else, and it behaves as it should, so it must be something I've done/not done that triggers my problem. I'll post ActivityB's source in a little bit, thanks. – user3571852 Oct 08 '14 at 18:41
0

OK, thanks to everyone that pointed me in the right direction. I found the solution. The issue was that I was doing startActivityForResult() in a DialogFragment so I should have done

    getActivity().startActivityForResult()

instead of just

    startActivityForResult()

then it works as expected.