0

I have two fragments with names F1 and F2 and an activity named A. I added fragment F1 in a Framelayout inside activity A using something like this:

F1 fragment = new F1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_drawer_frame, F1, LEFT_DRAWER_FRAGMENT);
transaction.commit();

Then inside fragment F1 I added fragment F2 in another layout:

F2 fragment = new F2();
fragment.setTargetFragment(this, 0); //"this" refer to an instance of F1
FragmentTransaction transacton = getChildFragmentManager().beginTransaction();
transacton.replace(R.id.drawer_host_frame, fragment, CURRENT_FRAGMENT);
transacton.commit();

Then I start an Activity from F2 using startActivityForResult():

startActivityForResult(getActivity(), intent, REQUEST_ACCOUNT);

And then I overrided onActivityResult() inside fragment F2. but the problem is onActivityResult() is never called. then I figured out that super.onActivityResult() inside fragment F1 don't trigger onActivtyResult() inside fragment F2.

I can manually retrieve every Fragment inside getChildFragmentManager() and then call onActivityResult() on them, but its clumsy and is like I'm just leaving the real problem unsolved.

Blo
  • 11,903
  • 5
  • 45
  • 99
user2136334
  • 648
  • 7
  • 16

2 Answers2

3

As I said one way is to use something like this inside fragment F1:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Here catch every requestCode for parentFragment
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getChildFragmentManager().getFragments())
       if (fragment != null)
           fragment.onActivityResult(requestCode, resultCode, data);
}
user2136334
  • 648
  • 7
  • 16
0

Parent fragment F1 will receive onActivityResult,F2 will not. F1 can find F2 fragment and then call its method to get the tasks done.

Shinoo Goyal
  • 601
  • 8
  • 10