onActivityResult not called in nested Fragment

When startActivityForResult is called in a nested fragment or a custom view in a nested fragment, and the onActivityResult is not getting called in the fragment. The solution to is to explicitly call the onActivityResult in the Activity class that is housing the fragments.

In the Activity class, get the fragments by getSupportFragmentManager().getFragments() and explicitly call onActivityResult in each fragment.

@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

In the fragment, do the things.

@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1234 && resultCode == 1) {
    	// do your things
    }
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search