欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android中两个类让你再也不用实现onActivityResult()

时间:2021-01-11 11:01:22|栏目:Android代码|点击:

前言

当我们知道fragment仅仅可以收到自己请求的回调后,我们就可以用它来解耦activity中onActivityResult的逻辑了。一般情况下,如果一个activity中多个回调结果的话,onActivityResult()中会有各种case判断,并且这种设计也打断了流式写法。那么我们索性建立一个无UI的fragment做精准的接收器,这样就可以再也不写onActivityResult()了。

Fragment:

/**
 * @author Kale
 * @date 2018/4/13
 */
public class OnActResultEventDispatcherFragment extends Fragment {

 public static final String TAG = "on_act_result_event_dispatcher";

 private SparseArray<ActResultRequest.Callback> mCallbacks = new SparseArray<>();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setRetainInstance(true);
 }

 public void startForResult(Intent intent, ActResultRequest.Callback callback) {
  mCallbacks.put(callback.hashCode(), callback);
  startActivityForResult(intent, callback.hashCode());
 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  ActResultRequest.Callback callback = mCallbacks.get(requestCode);
  mCallbacks.remove(requestCode);

  if (callback != null) {
   callback.onActivityResult(resultCode, data);
  }
 }
}

请求封装类:

public class ActResultRequest {

 private OnActResultEventDispatcherFragment fragment;

 public ActResultRequest(Activity activity) {
  fragment = getEventDispatchFragment(activity);
 }

 private OnActResultEventDispatcherFragment getEventDispatchFragment(Activity activity) {
  final FragmentManager fragmentManager = activity.getFragmentManager();

  OnActResultEventDispatcherFragment fragment = findEventDispatchFragment(fragmentManager);
  if (fragment == null) {
   fragment = new OnActResultEventDispatcherFragment();
   fragmentManager
     .beginTransaction()
     .add(fragment, OnActResultEventDispatcherFragment.TAG)
     .commitAllowingStateLoss();
   fragmentManager.executePendingTransactions();
  }
  return fragment;
 }

 private OnActResultEventDispatcherFragment findEventDispatchFragment(FragmentManager manager) {
  return (OnActResultEventDispatcherFragment) manager.findFragmentByTag(OnActResultEventDispatcherFragment.TAG);
 }

 public void startForResult(Intent intent, Callback callback) {
  fragment.startForResult(intent, callback);
 }

 public interface Callback {

  void onActivityResult(int resultCode, Intent data);
 }
}

发送请求和监听请求的代码:

Intent intent = new Intent(this, SecondActivity.class);

request.startForResult(intent, new ActResultRequest.Callback() {
 @Override
 public void onActivityResult(int resultCode, Intent data) {
  Toast.makeText(MainActivity.this, "" + resultCode, Toast.LENGTH_SHORT).show();
 }
});

题外话:

最新的support包已经解决了fragment嵌套时里层的fragment收不到结果的问题,请及时更新依赖。

总结

上一篇:Android数据加密之Des加密详解

栏    目:Android代码

下一篇:Android抽象布局――include、merge 、ViewStub详解

本文标题:Android中两个类让你再也不用实现onActivityResult()

本文地址:http://www.codeinn.net/misctech/43635.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有