ActivityResult 让你优雅的获取Activity的返回值

96
liwg
2018.01.05 16:24* 字数 236

github https://github.com/lwugang/ActivityResult

allprojects {
    repositories {
        maven { url 'https://www.jitpack.io' }
    }
}
dependencies {
    compile 'com.github.lwugang:ActivityResult:ef8a6afcaf'
}

大家都知道在一个APP中,Activity之间的数据交互是经常的,

传统方式获取Activity返回值,这种方式需要操作多个方法,可读性很差

 startActivityForResult(new Intent(),1);
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //获取数据
  }

ActivityResult方式 只需关注一个方法,实现方法的链式调用,可读性强,对代码无侵入,只需修改打开方式,另一个界面的逻辑无需特殊处理

ActivityResult.of(this)
        .className(TestActivity.class)//目标Activity类名
        //绿色通道,不走任何拦截器,直接打开目标
        .greenChannel()
        //transition 动画
        .options(ActivityOptionsCompat.makeScaleUpAnimation(v,(int)v.getX(),(int)v.getY(),
            v.getWidth()/2,v.getHeight()/2).toBundle())
        //拦截器,在startActivity之前执行
        .intercept(new Intercept() {
          @Override public boolean onIntercept(Activity activity, ActivityResult activityResult) {
            Log.e(TAG, "onIntercept: test" );
            //拦截器,return true 表示拦截,之后的拦截器都不会执行
            //activityResult.onContinue(); 继续执行之后的逻辑
            return false;
          }
        }).forResult(new ActivityResultListener() {
      @Override public void onReceiveResult(int resultCode, Intent data) {
          // 结果处理,也就是onActivityResult方法一样
      }
    });

Application 中提前加入拦截器(可选)

//注册拦截器
ActivityResultManager.get()
        .registerIntercept(new Intercept() {
          @Override public boolean onIntercept(Activity activity,final ActivityResult activityResult) {
            Log.e(TAG, "onIntercept: " );
            new Thread(){
              @Override public void run() {
                super.run();
                SystemClock.sleep(2000);
                activityResult.onContinue();
              }
            }.start();
            return true;
          }
        // true 表示使用一次之后就自动移除,下一次不会在执行此拦截器
        },true).registerIntercept(new Intercept() {
      @Override public boolean onIntercept(Activity activity, ActivityResult activityResult) {
        //可以判断是否登录成功,返回对应的值
        Log.e(TAG, "onIntercept2222222222: " );
        return false;
      }
    },true);

实现分析

参考 RxPermissions 的实现方式
Activity的结果获取总是离不开系统的方式的,ActivityResult只是对系统的方式进行了一层包装,通过使用Fragment来代理,在fragment的 startActivityForResult方法来实现Activity的跳转,在fragment中的onActivityResult方法,来回调我们的接口

ActivityResult.java 核心逻辑

 /**
   * 通过TAG查找ActivityResultFragment
   */
  Fragment findActivityResultFragment(Activity activity) {
    return activity.getFragmentManager().findFragmentByTag(TAG_ACTIVITY_RESULT_FRAGMENT);
  }

  private ActivityResultFragment getActivityResultFragment(Activity activity) {
    Fragment activityResultFragment = findActivityResultFragment(activity);
  //等于null表示,我们没有在当前Activity加入fragment,
    if (activityResultFragment == null) {
      //创建一个ActivityResultFragment对象,通过FragmentManager#add方法添加到当前Activity中
      FragmentManager fragmentManager = activity.getFragmentManager();
      activityResultFragment = new ActivityResultFragment();
      fragmentManager.beginTransaction()
          .add(activityResultFragment, TAG_ACTIVITY_RESULT_FRAGMENT)
          .commitAllowingStateLoss();
      fragmentManager.executePendingTransactions();
    }
    return (ActivityResultFragment) activityResultFragment;
  }
/**
   * 以返回值方式打开对应的界面
   * @param activityResultListener 可以为 null,null表示不需要处理返回值
   */
  public void forResult(@Nullable ActivityResultListener activityResultListener) {
    //给fragment设置我们的回调监听
    resultFragment.setActivityResultListener(activityResultListener);
  //绿色通道不走拦截器
    if (!greenChannel) {
      execIntercepts();
    } else {
      startActivity();
    }
  }

ActivityResultFragment.java

/**
 * @author lwg
 * @e-mail 13480020053@163.com
 * @time 1/4/18
 * @desc activity 结果代理fragment
 */

public class ActivityResultFragment extends Fragment {

  private ActivityResultListener activityResultListener;

  public ActivityResultFragment() {
  }

  public void setActivityResultListener(ActivityResultListener activityResultListener) {
    this.activityResultListener = activityResultListener;
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //回调我们的结果监听
    if(activityResultListener!=null)
      activityResultListener.onReceiveResult(resultCode, data);
  }
}

更多博客内容请关注:http://boke.liwg.top/

日记本