美文网首页Android进阶之路Android开发
基于透明fragment的长流程封装技巧

基于透明fragment的长流程封装技巧

作者: Android架构木木 | 来源:发表于2019-05-09 22:01 被阅读1次

是否厌烦了写各种onActivityResult?不要急,透明fragment来帮你

虽然fragment坑死人不偿命,但是不写view,只作为辅助功能来使用,还是很好用的.

基类

public abstract class BaseTransFragment extends Fragment {

    protected FragmentActivity activity;//自行保存activity引导,绝对不用getActivity()这个坑货

    protected void setHostActivity(FragmentActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);//避免旋转时fragment销毁重建
    }
}

工具方法:

public static <T extends BaseTransFragment> T getTransFragment(FragmentActivity fragmentActivity,
            String fragmentTag, Callable<T> newIntance) {
        try {
            FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
            T fragment = findFragment(fragmentManager, fragmentTag);
            boolean isNewInstance = fragment == null;
            if (isNewInstance) {
                fragment = newIntance.call();
                fragmentManager.beginTransaction()
                        .add(fragment, fragmentTag)
                        .commitAllowingStateLoss();//避免数据保存和恢复导致的crash
                fragmentManager.executePendingTransactions();
            }
            fragment.setHostActivity(fragmentActivity);
            return fragment;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                return newIntance.call();
            } catch (Exception e1) {
                e1.printStackTrace();
                return null;
            }
        }

    }

    private static <T extends BaseTransFragment> T findFragment(FragmentManager fragmentManager, String fragmentTag) {
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTag);
        if (fragment == null) {
            return null;
        }
        return (T) fragment;
    }

例子:

1.写了一个VendorPayTransFragment继承basefragment,没有界面,里面封装好了整套流程,处理了各种onactivityresult,权限,网络请求等等,然后,提供一个静态方法,提供输入和回调:

public static void startOrder(FragmentActivity activity, BaseQRPayCalInfo info, IVendorPayCallback callback) {
        VendorPayTransFragment fragment = getTransFragment(activity, "offlineorder",
                new Callable<VendorPayTransFragment>() {
            @Override
            public VendorPayTransFragment call() throws Exception {
                return VendorPayTransFragment.newInstance(info);
            }
        });
        fragment.setCallback(callback);
        fragment.startScanOrder();
    }

2.拍照/图片选择-裁剪-压缩整个流程,封装成一个工具方法:


TakePhotoUtil.startPickOne(this, false, new TakeOnePhotoListener() {
            @Override
            public void onSuccess(String s) {
               showImg(filePath);
            }

            @Override
            public void onFail(String path, String msg) {
                MyToast.errorBigL(s1);
            }

            @Override
            public void onCancel() {
            }
        });

相关文章

网友评论

    本文标题:基于透明fragment的长流程封装技巧

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