I'm trying to use startActivityForResult()
, I need to start an activity from a fragment, and then process the result in the original activity rather than the fragment that started it (when back is pressed). Can anyone tell me if this is possible or what my issue is?
Activity_A hosts Fragment_A, and the fragment can start Activity_B.
Fragment_A:
Intent intent = new Intent(mContext, Activity_B.class);
intent.putExtra(EXTRA_CHOSEN_BUSINESS, mChosenBusiness);
intent.putExtra(EXTRA_CHOSEN_POSITION, position);
getActivity().startActivityForResult(intent, 1);
Activity_B:
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("myBoolean", wasUsingMap);
setResult(RESULT_OK, intent);
finish();
super.onBackPressed();
}
Activity_A:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
mWasUsingMap = data.getBooleanExtra("myBoolean", false);
}
}
}
The data in this case is always null.
For testing I have tried not using getActivity().startActivityForResult(intent, 1);
and simply using startActivityForResult(intent, 1);
and the placing the onActivityResult in the fragment (with a call to the super onActivityResult in the activity following this answer) but the data is still null. I need it in the hosting activity anyway though if possible. I have also tried the top answer here and am still stumped..
(as a side note the boolean is used to detect which fragment should be displayed when the user presses back).