1

I have a parent fragment and in that there are three nested child fragments. I want to call the onActivityResult() on my child fragment.

I am aware that for nested fragments onActivityResult() is not called, so we need to explicitly call it from the parent fragment.

So In my parent fragment I have overridden the onActivityResult:-

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    List<Fragment> fragments = getChildFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
                fragment.onActivityResult(requestCode, resultCode, intent);
        }
    }
} 

But the issue is, even when I am working on just one child fragment, the onActivityResult() gets called for all the three nested fragments.

From each of the child fragment I am launching system activity(contact picker) and where I cannot set and intent extra, it returns null (system activities will not send back the extras with which they're called). So is there a way to identify a particular child fragment and just run the onActivityResult() for the nested fragment, for which I click the button. Following is the code in my nested fragment.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    contactPickerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent contactPickerIntent = new Intent(ACTION_GET_CONTENT);
            contactPickerIntent.setType(CONTENT_ITEM_TYPE);
            getParentFragment().startActivityForResult(contactPickerIntent, PICK_CONTACT_REQUEST_ID);
        }
    });
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
uff simon
  • 150
  • 2
  • 6

2 Answers2

1

Fragments are anyhow active. In that case, you can just store the nested fragment id in your onActivityCreated() method as:

FRAGMENT_ID=((RelativeLayout)view.getParent()).getId(); // cast it to your parent view

and then you can check the same in onActivityResult of nested fragment.Something like this

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode == REQUEST_ID) && 
            (resultCode == RESULT_OK) &&
            (getId() == FRAGMENT_ID)) {
            // your logic here
        }
    }

There can be a better way, but this will surely work. Hope this helps.

Nicks
  • 16,030
  • 8
  • 58
  • 65
0

You can give a different requestCode for each fragment in your startActivityForResult and check for the same requestCode and process it.

Naresh
  • 3,174
  • 1
  • 17
  • 22
  • Thanks for ur response but actually I am using same code for all the three nested fragments, how can I assign a different request code based on a particular fragment? – uff simon Jul 14 '15 at 02:30
  • I didn't get your point. If you have 3 fragments, then you can set the requestCode for each fragment separately. If you are using the same code, then just change the requestCode for each fragment in your startActivityForResult method. – Naresh Jul 15 '15 at 05:01
  • Actually, I was having just a single fragment(containing a button), (single xml and single java fragment class)and I was using it three times with different ids, so i was not able to set three different unique requestCode in button click. I tried setting requestcode= fragment id but there was a size restriction in that. Thanks for your help. – uff simon Jul 29 '15 at 04:55