Fragment中使用onActivityResult

使用场景:
ActivityA中含有Fragment A在FragmentA中跳转到ActivityB中。这个时候onActivityResult是无效的,没有执行到。
解决方案:
在BaseActivty中添加如下代码 (Base类要继承FragmentActivity) 添加完之后发现 Fragment中的onActivityResult生效了
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("BaseActivity onActivityResult");
FragmentManager fragmentManager = getSupportFragmentManager();
for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
Fragment fragment = fragmentManager.getFragments().get(i);
if (fragment == null) {
FontLog.i(getClass(), Integer.toHexString(requestCode));
} else {
handleResult(fragment, requestCode, resultCode, data);
}
}
}
private void handleResult(Fragment fragment, int requestCode, int resultCode, Intent data) {
fragment.onActivityResult(requestCode, resultCode, data);//调用每个Fragment的onActivityResult
List childFragment = fragment.getChildFragmentManager().getFragments(); //找到第二层Fragment
if (childFragment != null)
for (Fragment f : childFragment)
if (f != null) {
handleResult(f, requestCode, resultCode, data);
}
if (childFragment == null) {
FontLog.i(getClass(), "MyBaseFragmentActivity is null");
} }

你可能感兴趣的:(Fragment中使用onActivityResult)