onActivityResult 解耦 不需要一层一层的写

public abstract class AbsShareMessageTemplates implements IShareMessageTemplates {
    public final static String SHARE_EMPTY_FRAGMENT_TAG = "share_empty_fragment";

    public AbsShareMessageTemplates(Activity activity){
        if (activity instanceof FragmentActivity) {
            FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
            LifeCycleFragment emptyFragment = new LifeCycleFragment();
            emptyFragment.setIShareMessageTemplates(this);
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.add(emptyFragment, SHARE_EMPTY_FRAGMENT_TAG).commitAllowingStateLoss();
        }
    }

    public static class LifeCycleFragment extends Fragment{
        private IShareMessageTemplates iShareMessageTemplates;

        public void setIShareMessageTemplates(IShareMessageTemplates iShareMessageTemplates) {
            this.iShareMessageTemplates = iShareMessageTemplates;
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (iShareMessageTemplates != null) {
             iShareMessageTemplates.handleActivityResult(requestCode, resultCode, data);
            }
        }
    }
}

 

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fm = getSupportFragmentManager();
        if(fm.getFragments()!=null && !fm.getFragments().isEmpty()) {
            for (Fragment fragment : fm.getFragments()) {
                if (null != fragment&&TextUtils.equals(fragment.getTag(), AbsShareMessageTemplates.SHARE_EMPTY_FRAGMENT_TAG)) {
                    fragment.onActivityResult(requestCode, resultCode, data);
                }
            }
        }
    }

 

posted on 2020-05-20 14:46  赵子隆  阅读(189)  评论(0编辑  收藏  举报

导航