温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-13 04:25:03 来源:脚本之家 阅读:252 作者:天之界线2010 栏目:移动开发

前言

当我们知道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收不到结果的问题,请及时更新依赖。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI