美文网首页
【工具】onActivityResult的简易处理

【工具】onActivityResult的简易处理

作者: 编外程序猿 | 来源:发表于2021-01-27 17:47 被阅读0次

    简介

    startActivityForResult相信大家都很熟悉,Activity数据回传的常用方式,配合onActivityResult使用。但是startActivityForResult的调用场景千千万,onActivityResult的处理都集中在一处,所以这两块逻辑是分离的关系。
    借鉴Glide添加空Fragment监听生命周期的方式,可以实现startActivityForResult和onActivityResult合并于一处处理。逻辑上会更清晰一些。

    具体实现

    关键代码其实就下面这个方法

    private void addSupportFragment(){
            if (isActive()){
                FragmentManager fragmentManager = mActivityRef.get().getFragmentManager();
                android.app.Fragment fragment = fragmentManager.findFragmentByTag(mFragmentTag);
                if (fragment != null && fragment instanceof SupportFragment){
                    mSupportFragment = (SupportFragment) fragment;
                }else {
                    mSupportFragment = new SupportFragment();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(mSupportFragment,mFragmentTag);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        fragmentTransaction.commitNow();
                    }else {
                        fragmentTransaction.commitAllowingStateLoss();
                        fragmentManager.executePendingTransactions();
                    }
                }
            }
        }
    

    在SupportFragment的onActivityResult处理接口回调

    @Override
        public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
            if (mBuilder != null && mBuilder.isActive()){
                if (mBuilder.getResultCallBack() != null){
                    mMainHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (mBuilder.getResultCallBack() == null){
                                return;
                            }
                            if (mBuilder.getResultThreadType() == ThreadType.THREAD_MAIN){
                                mBuilder.getResultCallBack().callBack(requestCode, resultCode, data);
                            }else {
                                ThreadPoolByFIFOQueue.getExecutor().execute(getCallBackRunnable(mBuilder.getResultCallBack(),requestCode, resultCode, data));
                            }
                        }
                    },mBuilder.getResultCallBackDelay());
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    
    private Runnable getCallBackRunnable(final IEasyResultCallback callback, final int requestCode, final int resultCode, final Intent data){
            return new Runnable() {
                @Override
                public void run() {
                    if (callback != null){
                        callback.callBack(requestCode, resultCode, data);
                    }
                }
            };
        }
    

    正常使用只需要callback.callBack(requestCode, resultCode, data);这个代码即可,之所以这么多行代码,完全是我闲的蛋疼加了一些可有可无的能力
    比如:
    延迟执行
    延迟回调
    回调的方法切换线程

    就这吧,就是一种思路,没几行代码
    源码

    相关文章

      网友评论

          本文标题:【工具】onActivityResult的简易处理

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