美文网首页
嵌套多层Fragment onActivityResult处理

嵌套多层Fragment onActivityResult处理

作者: WotYang | 来源:发表于2018-11-17 15:59 被阅读0次

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);
                }
            }
        }
    }

相关文章

网友评论

      本文标题:嵌套多层Fragment onActivityResult处理

      本文链接:https://www.haomeiwen.com/subject/ofqifqtx.html