Fragment使用 startActivityForResult(intent, requestCode);
baseActivity处理如下:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Activity.RESULT_OK == resultCode) {
FragmentManager fragmentManager = getSupportFragmentManager();
for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
Fragment fragment = fragmentManager.getFragments().get(i);
if (fragment != null) {
handleResult(fragment, requestCode, resultCode, data);
}
}
}
}
/**
* 递归调用每个Fragment的onActivityResult
*/
private void handleResult(Fragment fragment, int requestCode, int resultCode, Intent data) {
fragment.onActivityResult(requestCode, resultCode, data);
List<Fragment> childFragment = fragment.getChildFragmentManager().getFragments();
if (childFragment != null) {
for (Fragment f : childFragment) {
if (f != null) {
handleResult(f, requestCode, resultCode, data);
}
}
}
}
网友评论